【问题标题】:Android Service stops just after closing the appAndroid 服务在关闭应用程序后立即停止
【发布时间】:2017-09-20 12:07:03
【问题描述】:

创建了一个IntentService 并将for loop 放入onHandleIntent 方法中。每次我关闭应用程序(从最近的不强制关闭中删除)它都会停止。但是onDestroy 没有打电话。 我也尝试过不同的设备。我不认为这是内存不足的问题。
那么服务是否意味着仅在应用程序处于前台时才使用?
我必须在主线程的后台执行一些任务当用户关闭应用程序时,服务也接近了。
这是我的示例代码

 public class MyIntentService extends IntentService {


    private static final String TAG = "MyIntentService";

    public MyIntentService() {
        super("MyIntentService");
    }


    @Override
    protected void onHandleIntent(Intent intent) {

        for (int i = 0; i < 30; i++) {
            Log.d(TAG, "onHandleIntent:   " + i);
            try {
                Thread.sleep(600);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: ");
    }
}

参考:How to keep an IntentService running even when app is closed?
Service restarted on Application Close - START_STICKY

【问题讨论】:

    标签: android multithreading service intentservice android-recents


    【解决方案1】:

    关闭应用后使用以下代码重启服务

    public class MyService extends Service {
    
    @Override
    public int onStartCommand(final Intent intent, final int flags,
                              final int startId) {
        super.onStartCommand(intent, flags, startId);
        return Service.START_STICKY;
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Intent restartService = new Intent(getApplicationContext(),
                this.getClass());
        restartService.setPackage(getPackageName());
        PendingIntent restartServicePI = PendingIntent.getService(
                getApplicationContext(), 1, restartService,
                PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 100, restartServicePI);
        Toast.makeText(this, "onTaskRemoved", Toast.LENGTH_SHORT).show();
        super.onTaskRemoved(rootIntent);
    }}
    

    上述方法onTaskRemoved 100 毫秒后重新启动您的服务。

    【讨论】:

    • 我为什么要使用 onTaskRemoved。除非内存变低,否则不应终止服务。根据 Android 文档,这是默认功能。
    【解决方案2】:
    @Override
        public void onTaskRemoved(Intent rootIntent) {
    }
    

    从最近删除应用程序时将调用上述方法。但不会有上下文。因此,您需要在上下文可用时完成您的任务。所以把代码放在里面,

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        
       //do your operations
        return START_REDELIVER_INTENT;
    }
    

    请记住,在 onStartCommand 中,您应该返回 START_REDELIVER_INTENT 或 START_STICKY。你可以得到不同from here.

    还有一件事,只有当 startService 从代码的任何位置调用一次时,onStartCommand 才会自动撤销。

    所以,通过调用来运行服务

    startService(new Intent(context, serviceName.class));

    如果服务没有停止,上面的代码 onStartCommand 将被定期撤销。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 2014-04-21
    相关资源
    最近更新 更多