Sre*_*v R 1 notifications android callback ondestroy
我有两个标签的活动.单击两个选项卡将更改选项卡下方的片段.当该活动在前面时,我发出通知,之后我最小化应用程序并杀死该活动(而不是强制停止).
我的问题是,onDestroy当活动被用户杀死时,我没有回电话.现在,如果我点击通知,应用程序将强制关闭,因为缺少未决意图的活动.为什么不在onDestroy中收回回电?
我找到了解决方法:
创建服务:
public class MyService extends Service {
    @Override
    public final int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }
    @Override
    public final IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Toast.makeText(getApplicationContext(), "APP KILLED", Toast.LENGTH_LONG).show(); // here your app is killed by user
        try {
            stopService(new Intent(this, this.getClass()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            super.onTaskRemoved(rootIntent);
        } else{}
    }
}
然后在应用开始时启动您的服务:
startService(new Intent(this, MyService.class));
确保在AndroidManifest.xml中注册服务
<service
            android:enabled="true"
            android:name="yourPackageName.MyService"
            android:stopWithTask="false" />
| 归档时间: | 
 | 
| 查看次数: | 4249 次 | 
| 最近记录: |