关闭应用程序后Android重新启动服务

问题描述:

我目前正在开发一个依靠背景service的电子邮件应用程序,以便能够自动获取新电子邮件。当应用程序打开时(或在应用程序的运行列表中),此功能完美运行,但只要关闭应用程序/将其从最近的应用程序列表中删除,service也会停止。这可以通过进入设备上的“开发人员设置”并查看没有任何流程或服务在运行我的应用程序来确认。关闭应用程序后Android重新启动服务

我已经阅读了*上无数的线程,但他们似乎都没有做到这一点。有时会调用onTaskRemoved(),并重新启动服务,但其他时间根本不调用或调用该服务,日志显示操作系统已安排重新启动service,但service由操作系统获得forced closed,而我总是需要运行此服务来检索新电子邮件。

我当前的代码可以在下面看到:

我的服务:

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

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     super.onStartCommand(intent, flags, startId); 

     Log.i(TAG, "onStartCommand()"); 

     ... 

     Log.d(TAG, "SERVICE IS RUNNING"); 

     if (host != null) { 
      Log.d(TAG, "STARTING PUSH SERVICE"); 

      Handler handler = new Handler(); 
      Runnable runnable = new Runnable() { 
       @Override 
       public void run() { 
        Log.d(TAG, "BR: " + ((BugReporting) getApplication())); 
        sharedRunnable = ((BugReporting) getApplication()).getSharedRunnable(); 

        MailPush mailPush = new MailPush(getApplicationContext(), sharedRunnable); 
        Log.d(TAG, "sharedRunnable 1: " + sharedRunnable); 
        mailPush.checkInboxEmail(host, email, password); 
        //mailPush.checkSentEmail(host, email, password); 
       } 
      }; 
      handler.postDelayed(runnable, 5000);//0.5 seconds 

     } 

     return START_STICKY; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Log.i(TAG, "onCreate()"); 
    } 

    @Override 
    public void onTaskRemoved(Intent rootIntent) { 
     super.onTaskRemoved(rootIntent); 
     Log.i(TAG, "onTaskRemoved()"); 

     PendingIntent service = PendingIntent.getService(
       getApplicationContext(), 
       1001, 
       new Intent(getApplicationContext(), MyService.class), 
       PendingIntent.FLAG_ONE_SHOT); 

     AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
     alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, service); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.i(TAG, "onDestroy()"); 
     startService(new Intent(this, MyService.class)); 
    } 

重启接收机:

public class AutoStart extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     context.startService(new Intent(context, MyService.class)); 
    } 
} 

清单:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

... 

     <receiver 
       android:name="uk.co.tundracorestudios.securemail.notification.AutoStart" 
       android:enabled="true" 
       android:exported="true" 
       android:process=":remote"> 
       <intent-filter> 
        <action android:name="android.intent.action.BOOT_COMPLETED" /> 
       </intent-filter> 
      </receiver> 

      <service 
       android:name="uk.co.tundracorestudios.securemail.notification.MyService" 
       android:enabled="true" 
       android:exported="true" 
       android:label="@string/mail_service" 
       android:stopWithTask="false"/> 

我没有尝试运行MyService为不同process但这则限制我获得我已经保存在应用程序类,否则可通过访问sharedRunnable

updateListRunnable = ((BugReporting) getApplication()).getSharedRunnable(); 

因此,我问,如何确保我的service即使在应用程序关闭时或设备为rebooted时仍能连续运行/工作,同时仍可访问位于getApplication()的组件?

,当我试图运行MyService作为一个单独的process,上述updateListRunnable总是会返回null而当应用程序和service在同一process正在运行,它会返回正确的runnable

我面临着类似的问题而回,在我的服务的onDestroy()方法中使用这样的:

public void onDestroy() { 
    Intent restartService = new Intent(getApplicationContext(),this.getClass()); 
    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),1,restartService,PendingIntent.FLAG_ONE_SHOT); 
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    alarmManager.set(AlarmManager.ELAPSED_REALTIME,5000,pendingIntent); 
    super.onDestroy(); 
} 

当服务被destoyed予组5秒的警报,这将启动我的服务aagain。这样,当用户从最近移除它时,你的服务将停止,但它将重新开始。

+0

正确。 'onTaskRemoved'不是服务生命周期方法的一部分:https://developer.android.com/guide/components/services.html#Lifecycle 'onTaskRemoved'描述在这里https://developer.android.com/reference /android/app/Service.html#onTaskRemoved(android.content.Intent) – Ryan

+0

谢谢nullvoid!我已经从字面上尝试了互联网上提供的每个解决方案,这已经解决了我的问题,即强制关闭应用程序未收到通知。几乎1个月的斗争!感谢您分享您的解决方案! – Lozy

+0

它很高兴,它帮助。 @Lozy你可以把这个标记为答案,如果这对你有用。 – nullvoid