gpt4 book ai didi

android - 长时间运行的后台进程

转载 作者:太空狗 更新时间:2023-10-29 16:33:24 26 4
gpt4 key购买 nike

我想要的运行一个后台服务,在 1 分钟的固定间隔后通过 RESTFUL 网络服务与我们的服务器通信(这是一个严格的项目要求。所以不能增加它。)

我有什么:我尝试了各种方法,使用简单的 BroadcastReceivers 和简单的 ServiceWakefulBroadcastReceivers 使用 < strong>WakefulIntentService 等。

主要问题:主要问题是当设备屏幕打开时,一切正常/固定间隔工作正常,但当屏幕关闭或设备被锁定时,警报管理器会触发服务5 分钟 的最小间隔。这正是我不想要的。当设备锁定/屏幕关闭时,我想要相同的 1 分钟间隔。

下面是我的代码:

list .xml

  <uses-permission android:name="android.permission.WAKE_LOCK" />   
<receiver android:name=".MyScheduledReceiver" />
<service android:name=".BackgroundService" />

Activity.java

 MyScheduledReceiver.scheduleAlarms(MainActivity.this);  

渐变

// Background long running process
compile 'com.commonsware.cwac:wakeful:1.0.+'


repositories {
maven {
url "https://s3.amazonaws.com/repo.commonsware.com"
}

}

广播接收者

public class MyScheduledReceiver extends WakefulBroadcastReceiver {


private static final int PERIOD = 60 * 1000;
private static final int INITIAL_DELAY = 2000; // 5 seconds

@Override
public void onReceive(Context context, Intent i) {

try {

if (i.getAction() == null) {
WakefulIntentService.sendWakefulWork(context, BackgroundService.class);

} else {
scheduleAlarms(context);

}

} catch (Exception e) {
e.printStackTrace();
}
}


static void scheduleAlarms(Context ctxt) {

AlarmManager mgr = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(ctxt, MyScheduledReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(ctxt, 0, i, 0);

mgr.setRepeating(AlarmManager.RTC_WAKEUP, INITIAL_DELAY, PERIOD, pi);

}

}

后台服务

public class BackgroundService extends WakefulIntentService {


public BackgroundService() {
super("BackgroundService");
}

@Override
protected void doWakefulWork(Intent intent) {

sendNotification("HELLOO");

stopSelf();
}

@Override
public void onTaskRemoved(Intent rootIntent) {

MyScheduledReceiver.scheduleAlarms(BackgroundService.this);

super.onTaskRemoved(rootIntent);
}

private void sendNotification(String message) {


try {
Intent intent = intent = new Intent(this, MainActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification notification;
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("test")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

notification = notificationBuilder.build();
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

} catch (Exception e) {
e.printStackTrace();
}

}

}

目前,我只是在我的 BackgroundService 的通知栏中显示带有声音的通知。
我将衷心感谢您的帮助。谢谢!

最佳答案

反正Android 6.0不支持你想要的。得益于 Doze 模式,Android 6.0 不会每分钟向任何应用程序发出警报。

最接近的是:

  • 有一个服务使用 ScheduledExecutorService 来控制每一分钟来完成您的工作

  • 让该服务获取WakeLock 并始终保持 CPU 开启

  • 让该服务使用 startForeground()START_STICKY 来最大限度地减少它不在身边因此无法完成这项工作的时间

  • 确保应用程序已添加到“设置”中的“忽略电池优化”白名单

  • 忽略用户的痛苦呼喊,提示他们的电池生命周期很糟糕

关于android - 长时间运行的后台进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34291139/

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