警报管理器/服务/广播接收器关闭或从最近的应用程序中掠过后不起作用



我知道这有很多问题。我尝试了其中的大多数,并花了很多时间。但是没有得到任何解决方案

实际上,我想要一个背景过程来运行到无限时间,甚至从最近的应用中删除了应用程序我想在15分钟后重复访问用户的GPS位置

所以我首先尝试了

尝试1 ******我创建了广播接收器,他会打电话给我的服务该广播接收器的名称是LocationservicErestarter我正在启动警报中继器,他将定期致电5秒的时间间隔

Intent intent = new Intent("com.lmf.overduereport.gpstimer.LocationServiceRestarter");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
        0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am =
        (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
alarm_manager.setRepeating(AlarmManager.RTC_WAKEUP,SystemClock.elapsedRealtime()+5*1000,5*1000,pendingIntent);

如果我不从最近的应用中删除应用程序,它可以正常工作我在广播接收器中添加了一个日志,该日志在我的文件夹中写下了关于广播接收器是否打电话的登录。在这种情况下,广播接收器本身不是从警报管理器中调用的,因此显然不会调用我要打电话给记录位置的服务。

尝试2 ******我还尝试了可以无限运行的服务。但是它在应用程序关闭后也停止工作

尝试3 ******这次,我不会致电重复的警报管理器,而不是我遵循的

Intent intent = new Intent("com.lmf.overduereport.gpstimer.LocationServiceRestarter");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
        0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am =
        (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
//--
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        SystemClock.elapsedRealtime() + 5000, pendingIntent);

上面将致电广播接收器,在广播接收器中,我添加了警报管理器,如下所示

LocationServiceM.appendLog("Got Reciever******");
intent = new Intent("com.lmf.overduereport.gpstimer.LocationServiceRestarter");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
        12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am =
        (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        SystemClock.elapsedRealtime() + 5000, pendingIntent);

上面将使用新的时间表时间重新启动广播接收器,因此链将继续。

但可悲的是,这也行不通。如果未从最近的应用中删除应用程序,则可以正常工作。

即使我在服务和意图服务上都尝试过同样的事情,没有运气。

结论:从最近的应用中删除应用程序后,请未调用警报管理器或广播接收器。

,但我看到了许多在后台上运行的应用程序,甚至在最近的应用程序中都关闭了。

您可以使用start_sticky,它告诉操作系统有足够的内存后重新创建服务,并以无效的意图调用onstartcommand()。start_not_sticky告诉操作系统不要再重新创建服务。

您可以在关闭应用程序或从任务中杀死应用程序时使用服务来触发:

public class SensorService extends Service {
public int counter=0;
public SensorService(Context applicationContext) {
    super();
    Log.i("HERE", "here I am!");
}
public SensorService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    startTimer();
    return START_STICKY;
}
@Override
public void onDestroy() {
    super.onDestroy();
    Log.i("EXIT", "ondestroy!");
    Intent broadcastIntent = new Intent("uk.ac.shef.oak.ActivityRecognition.RestartSensor");
    sendBroadcast(broadcastIntent);
    stoptimertask();
}
private Timer timer;
private TimerTask timerTask;
long oldTime=0;
public void startTimer() {
    //set a new Timer
    timer = new Timer();
    //initialize the TimerTask's job
    initializeTimerTask();
    //schedule the timer, to wake up every 1 second
    timer.schedule(timerTask, 1000, 1000); //
}
/**
 * it sets the timer to print the counter every x seconds
 */
public void initializeTimerTask() {
    timerTask = new TimerTask() {
        public void run() {
            Log.i("in timer", "in timer ++++  "+ (counter++));
        }
    };
}
/**
 * not needed
 */
public void stoptimertask() {
    //stop the timer, if it's not already null
    if (timer != null) {
        timer.cancel();
        timer = null;
    }
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

您还需要一名广播员,当某人或某物杀死服务时,它将收到信号;它的作用是重新启动服务。

 public class SensorRestarterBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(SensorRestarterBroadcastReceiver.class.getSimpleName(), "Service Stops! Oooooooooooooppppssssss!!!!");
        context.startService(new Intent(context, SensorService.class));;
    }
}

有关完整实施的信息请参阅此链接http://fabcirablog.weebly.com/blog/creating-a-never-dend-background-service-in-android

最新更新