Android 几分钟后停止服务或锁定手机

Android 几分钟后停止服务或锁定手机,android,foreground-service,background-foreground,foregroundnotification,Android,Foreground Service,Background Foreground,Foregroundnotification,我尝试创建前台服务,每20秒发送一条消息,但这项服务在几分钟后停止或锁定手机。 我在安卓8(O)中测试了这项服务。 我的代码是: @Override public int onStartCommand(Intent intent, int flags, int startId) { input = intent.getStringExtra("inputExtra"); new Thread(new Runnable() { public void run() {

我尝试创建前台服务,每20秒发送一条消息,但这项服务在几分钟后停止或锁定手机。 我在安卓8(O)中测试了这项服务。 我的代码是:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
  input = intent.getStringExtra("inputExtra");

  new Thread(new Runnable() {
    public void run() {
      while (progressStatus < 10000000) {
        progressStatus += 1;           
        handler.post(new Runnable() {
          @SuppressLint("MissingPermission")
          public void run() {
            Intent notificationIntent = new Intent(ExampleService.this, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(ExampleService.this,
              0, notificationIntent, 0);
            String mmm = CHANNEL_ID;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
              id = "id_product";
              // The user-visible name of the channel.
              CharSequence name = "Product";
              // The user-visible description of the channel.
              String description = "Notifications regarding our products";
              int importance = NotificationManager.IMPORTANCE_MAX;
              NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_DEFAULT);
              // Configure the notification channel.
              mChannel.setDescription(description);
              mChannel.enableLights(true);
              // Sets the notification light color for notifications posted to this
              // channel, if the device supports this feature.
              mChannel.setLightColor(Color.RED);
              notificationManager.createNotificationChannel(mChannel);
            }

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(),"id_product")
              .setContentTitle("Example Service")
              .setContentText(input)
              .setSmallIcon(R.drawable.ic_launcher_background)
              .setContentIntent(pendingIntent)
              .setChannelId(id)

              .setAutoCancel(true).setContentIntent(pendingIntent)
              .setNumber(1)
              .setColor(255)
              .setContentText(input)
              .setWhen(System.currentTimeMillis());
            notificationManager.notify(1, notificationBuilder.build());

            Notification notification = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
              .setContentTitle("Example Service")
              .setContentText(input)
              .setSmallIcon(R.drawable.ic_launcher_background)
              .setContentIntent(pendingIntent)
              .build();

              Toast.makeText(getApplicationContext(), " run service" , Toast.LENGTH_SHORT).show();

              startForeground(1, notification);
            }
        });
        try {
          Thread.sleep(20000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }).start();

  return START_STICKY;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
  return null;
}
@覆盖
公共int onStartCommand(Intent Intent、int标志、int startId){
input=intent.getStringExtra(“inputExtra”);
新线程(newrunnable()){
公开募捐{
而(进度状态<10000000){
progressStatus+=1;
handler.post(新的Runnable(){
@SuppressLint(“丢失许可”)
公开募捐{
Intent notificationIntent=新意图(例如service.this、MainActivity.class);
PendingEvent PendingEvent=PendingEvent.getActivity(例如service.this,
0,通知意图,0);
字符串mmm=通道ID;
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O){
notificationManager=(notificationManager)getSystemService(通知服务);
id=“id\u产品”;
//频道的用户可见名称。
CharSequence name=“产品”;
//频道的用户可见描述。
String description=“关于我们产品的通知”;
int重要性=NotificationManager.importance\u MAX;
NotificationChannel mChannel=新的NotificationChannel(id、名称、NotificationManager.IMPORTANCE\u默认值);
//配置通知通道。
mcchannel.setDescription(描述);
mChannel.enableLights(真);
//设置发布到此服务器的通知的通知灯颜色
//通道,如果设备支持此功能。
mcchannel.setLightColor(Color.RED);
notificationManager.createNotificationChannel(MCChannel);
}
NotificationCompat.Builder notificationBuilder=新建NotificationCompat.Builder(getApplicationContext(),“id\U产品”)
.setContentTitle(“示例服务”)
.setContentText(输入)
.setSmallIcon(R.drawable.ic_启动器_背景)
.setContentIntent(挂起内容)
.setChannelId(id)
.setAutoCancel(true).setContentIntent(pendingIntent)
.setNumber(1)
.setColor(255)
.setContentText(输入)
.setWhen(System.currentTimeMillis());
notificationManager.notify(1,notificationBuilder.build());
Notification Notification=new NotificationCompat.Builder(getApplicationContext(),通道ID)
.setContentTitle(“示例服务”)
.setContentText(输入)
.setSmallIcon(R.drawable.ic_启动器_背景)
.setContentIntent(挂起内容)
.build();
Toast.makeText(getApplicationContext(),“运行服务”,Toast.LENGTH_SHORT.show();
启动前(1,通知);
}
});
试一试{
睡眠(20000);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
}).start();
返回开始时间;
}
@可空
@凌驾
公共IBinder onBind(意向){
返回null;
}

这项服务在开始和开始的几分钟内都可以,但通常在几分钟后结束,有时在几分钟后再次启动。有时通过连接互联网启动,有时通过连接USB电缆启动。我真的不知道原因是什么。有人能帮我吗?

请在, 后台服务

我的建议是使用:

  • 作业调度器

  • 工作经理

  • 希望这个答案能对你当前的代码有所帮助


    我必须说,该服务始终在我的设备的运行服务中运行。我创建了通知频道,并检查了android版本。您链接的所有推荐部分都在我的cod中,但几分钟后通知不会关闭,toast也不会重复。该服务始终在我的设备的运行服务中运行。一旦USB连接线连接好就开始工作真的很酷。因为api级别超过21,所以当我单击notification service重新开始工作时,OSS会终止后台服务。我应该怎么做才能停止OS终止服务?