gpt4 book ai didi

android 闹钟管理器在几个小时后停止

转载 作者:太空宇宙 更新时间:2023-11-03 10:41:50 25 4
gpt4 key购买 nike

我创建了一个每 5 秒启动一次服务的警报管理器,该应用程序运行良好但几个小时后警报管理器停止运行。请注意,该应用程序未被用户使用,这意味着它在没有用户交互的情况下在后台运行。有谁知道如何每隔几秒启动一次服务而不停止?

AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.TRUE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (1000 * 60 * 5), pi);

最佳答案

不要对警报管理器做这样的事情。报警管理器更多应该在几个小时内完成的任务的概念(在最佳情况下)。

如果您需要精确处理您的任务,请在内部使用处理程序服务,该帖子执行您的可运行。在您的 runnable 中,调用该方法以再次执行 runnable。

将runnable和handler保存为服务的成员变量并make过程很粘。如果你想停止服务和处理程序(处理程序不会如果你只是调用 stopService(Context) 或 Service.stopSelf(),你需要停止在处理程序上使用您的可运行对象调用 removeCallbacks(runnable)。

干杯。

顺便说一句:考虑一下您是否真的想要每 5 秒启动一次服务。也许只是在服务的可运行内部做服务工作我刚刚描述了。

Edit2:如果您需要服务可靠,请为启动操作添加一个 BroadcastReceiver,它将在启动时接收广播并重新启动您的服务。

Edit3:在不启动服务的情况下检查您的服务是否正在运行的代码:

    public static boolean isRunning(Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (YourService.class.getName().equals(service.service.getClassName())){
return true;
}
}
return false;
}

但是,尽管如此,请在 onStartCommand 中检查您的成员“处理程序”是否为空如果是这样,请忽略启动请求,因为这可能会导致您遇到多个可运行处理的情况。

编辑4:

服务代码 fragment :

public class LoopingServiceSnipped extends Service{

public static final long STANDARD_FREQUENCY = 1000;
private static long loopingFrequency = STANDARD_FREQUENCY;
private Handler serviceHandler;
private Runnable loop;

//EXTRAS

private static final String INTENT_EXTRA_STOP_SERVICE = LoopingServiceSnipped.class.getSimpleName() + "_INTENT_EXTRA_STOP_SERVICE";
private static final String INTENT_EXTRA_FREQUENCY = LoopingServiceSnipped.class.getSimpleName() + "_INTENT_EXTRA_FREQUENCY";

/**
* Determines if the service is running (reliably) without starting it.
*
* @param context
* needed for SystemServices
*
* @return true if the service is running
*/
public static boolean isRunning(Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (LoadingScreenAppDetectionService.class.getName().equals(service.service.getClassName())){
return true;
}
}
return false;
}

public static void stopService(Context c) {
Intent i = new Intent(c, LoopingServiceSnipped
i.putExtra(INTENT_EXTRA_STOP_SERVICE, true);
c.startService(i);
}

public static synchronized void setFrequency(Context c, long frequency) {
Intent i = new Intent(c, LoadingScreenAppDetectionService.class);
i.putExtra(INTENT_EXTRA_FREQUENCY, frequency);
c.startService(i);
}

@Override
public void onCreate() {
super.onCreate();
init();
}

private void init() {
//do your initialization here
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (shouldStopService(intent)){
LOG.d("Going to stop service!");
tryStopService();
return Service.START_NOT_STICKY;
} else {
return startOrContinueService(intent);
}
}

private void tryStopService() {
stopLooping();
stopSelf();
}

private void stopLooping() {
if (serviceHandler != null){
serviceHandler.removeCallbacks(getLoop());
serviceHandler = null;
}
}

private int startOrContinueService(Intent intent) {
if (hasNewFrequency(intent)){
setFrequency(intent);
stopLooping();
}
if (!isServiceRunning()){
startLooping();
}
} else {
LOG.d("Going to continue with service!");
}
return Service.START_STICKY;
}


private boolean hasNewFrequency(Intent intent) {
return intent.hasExtra(INTENT_EXTRA_FREQUENCY);
}

private void setFrequency(Intent intent) {
if (intent.hasExtra(INTENT_EXTRA_FREQUENCY)){
loopingFrequency = intent.getLongExtra(INTENT_EXTRA_FREQUENCY, STANDARD_FREQUENCY);
}
}

private boolean isServiceRunning() {
return serviceHandler != null;
}

private boolean shouldStopService(Intent i) {
if (i.hasExtra(INTENT_EXTRA_STOP_SERVICE)){
return i.getBooleanExtra(INTENT_EXTRA_STOP_SERVICE, false);
} else {
return false;
}
}

private void startLooping() {
if (serviceHandler == null){
serviceHandler = new Handler();
}
serviceHandler.postDelayed(getAppDetectionLoop(), getFrequency());
}

private Runnable getLoop() {
if (loop == null){
loop = new Runnable() {
@Override
public void run() {
//do your looping work here
startLooping();
}
};
}
return loop;
}

另外添加Boot Receiver和Screen TurnOn Receiver。

关于android 闹钟管理器在几个小时后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32146221/

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