如何在不停止的情况下每分钟运行Jobscheduler或Service? [英] How to run a Jobscheduler or a Service every minute wihout stopping?

查看:418
本文介绍了如何在不停止的情况下每分钟运行Jobscheduler或Service?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个Android应用,该应用需要经常发送其位置信息,最多每1分钟或2分钟发送一次.为此,我使用JobSchedulerService.通过将.setPeriodic()替换为.setMinimumLatency(),我已经设法使其在具有Android N version的设备上每15分钟运行一次以上.事实是,一开始它是在既定的时间内定期执行的,但过了一会儿,它大约每7或9分钟运行一次.

I'm doing an Android app that requires sending its location frequently, every 1 minute or 2 minutes at the most. For this, I use a JobSchedulerService. I've already managed to make it run more than once every 15 minutes on devices with Android N version by replacing the .setPeriodic() with a .setMinimumLatency(). The fact is that at the beginning it is executed periodically in the established time, but after a while it runs every 7 or 9 minutes approximately.

我已经将该应用程序包括在节电白名单中,但是没有用.有什么方法可以每分钟无限制地执行它或类似的服务吗?应用程序消耗多少电量都没关系.

I have already included the application in the battery saving white list, but didn't work. Is there any way to execute it or a similar service every minute with no restrictions? Doesn't matter how much battery the app spends.

编辑:

这是我尝试过的:

ReceiverService:

ReceiverService:

public class ReceiverService extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context ctx, Intent intent) {

    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        if (!isMyServiceRunning(ServiceBackground.class, ctx))
            startWakefulService(ctx, new Intent(ctx, ServiceBackground.class));

        new ServiceAlarmManager(ctx).register();
    }

}

private boolean isMyServiceRunning(Class<?> serviceClass,Context context) {
    ActivityManager manager = (ActivityManager)context. getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            Log.i("Service already","running");
            return true;
        }
    }
    Log.i("Service not","running");
    return false;
}

}

ServiceAlarmManager与@madking所说的完全相同.

The ServiceAlarmManager is exactly the same as @madking said.

推荐答案

您可以将发送位置的代码放在Service中,并实现一个AlarmManager来定期检查您的Service是否正在运行,并重新启动它. Service已被操作系统杀死.您必须使用WakefulBroadcastReceiver来实现AlarmManager.

You can put your code that sends location in a Service and implement an AlarmManager that periodically checks if your Service is running and restarts it if the Service has been killed by OS. You'll have to implement the AlarmManager using a WakefulBroadcastReceiver.

ReceiverService.java

ReceiverService.java

public class ReceiverService extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context ctx, Intent intent) {

    if (!YourService.isRunning()) {
        startWakefulService(ctx, new Intent(ctx, YourService.class));
    }

    new ServiceAlarmManager(ctx).register();
}
}

ServiceAlarmManager.java

ServiceAlarmManager.java

public class ServiceAlarmManager {

private Context ctx;
private static final int TIME_INTERVAL = 300 * 1000;

public ServiceAlarmManager(Context context) {
    ctx = context;
}

public void register() {

    Intent serviceRestarter = new Intent();
    serviceRestarter.setAction("someString");

    PendingIntent pendingIntentServiceRestarter = PendingIntent.getBroadcast(ctx, 0, serviceRestarter, 0);
    AlarmManager alarmManager = (AlarmManager) ctx.getSystemService(ctx.ALARM_SERVICE);
    Date now = new Date();
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, now.getTime() + TIME_INTERVAL, pendingIntentServiceRestarter);

}
}

还要在Manifest.xml文件中注册BroadcastReceiver

<receiver android:name=".ReceiverService">
        <intent-filter>
            <action android:name="someString" />
        </intent-filter>
    </receiver>

register()方法有两件事.

1-发出WakefulBroadcastReceiver捕获的广播,并在需要时重新启动Service

1- Issues a broadcast which is caught by WakefulBroadcastReceiver and restarts the Service if required

2-设置要调用的下一个警报,以检查Service是否已被杀死.

2- Sets the next alarm to be invoked to check if the Service has been killed.

通过这种方式,即使操作系统将其杀死,该服务也将继续运行,并且您将能够定期发送位置更新.

This way the service keeps running even if the OS kills it and you'll be able to send location updates periodically.

注意:尽管不建议您采用这种做法,因为您的应用程序会消耗更多的电池,但是您似乎并不在意,因为我也没有这样做,因为某些业务需求并没有给我们选择的余地.

Note: Though this practice is not recommended as your application will use more battery but you don't seem to care about it as I did not either as some business requirements don't leave us a choice.

这篇关于如何在不停止的情况下每分钟运行Jobscheduler或Service?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆