Android 在应用重新启动和关闭时终止服务?

Android kill service when app relauch and close?

我必须 运行 在达到计划时在后台执行一项任务 time.i 将唤醒我的服务的任务分配给 Alarm Manager 并且如果应用 background/running.service 保持 运行 如果在开始 service.for 之后不更改应用程序的状态,则在应用程序的背景和前景状态上保持正常 service.for 目的是很好地理解下面给出的场景。

案例一:

1.Setting up the schedule and sending the time to alarm manager and keep the app running.

2.Alarm manager calls the service when the time is reaching.

3.Service start running

4.when i close my app service get stopped

案例二:

1.Setting up the schedule and sending the time to alarm manager and closing the app.now app in background.

2.Alarm manager calls the service when the time is reaching.

3.Service start running

4.Relaunching the app.service continuing running/working fine.

5.Now closing the app and the result is service dead.

我只能通过 Alarm Manager 调用服务,而不是每次在服务 onStartCommand 方法中为此目的启动应用程序时我返回 START_NOT_STICKY 并且我不想要使用 START_STICKY。如果我去 START_STICKY 它不会考虑预定的 time.and 事情是我不想再次检查时间,因为它在 [=13] 之前做得很好=].

当应用程序 运行 在低内存上运行时,服务获得 destroyed.is 使用 startForeground 而不是 startService 的解决方案?

如何使我的服务 运行 稳定而不用担心应用 opening/closing 状态?

Setting schedule

public void setAction(View v) {
        Calendar calendar = Calendar.getInstance();
        int hour = Integer.parseInt(HH.getText().toString());
        int minute = Integer.parseInt(MM.getText().toString());
        int second = Integer.parseInt(SS.getText().toString());
        String peried = PP.getText().toString();
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);
        calendar.set(Calendar.AM_PM, peried.equalsIgnoreCase("AM") ? Calendar.AM : Calendar.PM);
        Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
        myIntent.putExtra(ACTION,
                ACTION_SC_1);
        pendingIntent = PendingIntent.getBroadcast(MainActivity.this, REQUEST_CODE_SC_1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
    }

Service class

public class MyService extends Service {
   

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            final String action = intent.getStringExtra(ACTION);
            final Handler handler = new Handler();
            final Timer timer = new Timer();
            final TimerTask doAsynchronousTask = new TimerTask() {
                @Override
                public void run() {
                    handler.post(new Runnable() {
                        public void run() {
                            try {
                                Toast.makeText(MyAlarmService.this, "Running....", Toast.LENGTH_LONG).show();

                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                            }
                        }
                    });
                }
            };
            timer.schedule(doAsynchronousTask, 1000, 5000); //execute in every 5000 msdo

          //  this.stopSelf();
        } catch (Exception e) {
           //TODO
        }

        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Toast.makeText(this, "Killed", Toast.LENGTH_LONG).show();
    }


 

有很多方法可以避免服务中断...但我们不能保证。在内存不足的情况下,服务将被 os 终止。在这种情况下,如果您 return START_STICKY 这将以空意图重新启动服务。如果您希望意图不为空 return START_REDELIVER_INTENT 请参阅 android 文档 here