gpt4 book ai didi

android - 几分钟或锁定手机后前台服务停止

转载 作者:搜寻专家 更新时间:2023-11-01 09:23:42 25 4
gpt4 key购买 nike

我尝试创建每 20 秒发送一条消息的前台服务,但该服务会在几分钟后或锁定手机后停止。我在 android 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;
}

此服务在开始和第一分钟都可以,但通常会在几分钟后结束,有时会在几分钟后再次启动。有时它通过连接到 Internet 启动,有时它通过连接 USB 电缆开始工作。我真的不知道是什么原因。有人可以帮我解决这个问题吗?

最佳答案

请在 here 中的某处正确阅读 Android Oreo 8.0 文档,用于后台服务

我的建议是使用:

  1. 作业调度器

  2. 工作经理

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

https://stackoverflow.com/a/48302378/6541643

关于android - 几分钟或锁定手机后前台服务停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52490363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com