从不调用Android服务的onTaskRemoved!为什么? [英] onTaskRemoved of a Android Service is never being called ! Why?

查看:579
本文介绍了从不调用Android服务的onTaskRemoved!为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近两天我一直遇到问题,我想点击api并在用户从后台任务中擦除应用时向用户显示Toast,我发现service可能是因为从后台删除应用程序时调用了onTaskRemoved.

I am stuck with a issue from last 2 days, I want to hit a api and show user a Toast when user swipe off the app from Background Task, I found that from service it is possible as onTaskRemoved is called when app is removed from background.

代码:

MyService.java

public class MyService extends Service {

private static final String TAG = MyService.class.getSimpleName();
Handler mHandler;

@Override
public void onCreate() {
    super.onCreate();
    mHandler = new Handler();
}

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "onStartCommand: ");
    mHandler.post(new Runnable() {
        @Override
        public void run() {
            new ToastPoP(MyService.this).showLongToast("Service started");
        }
    });

    return Service.START_STICKY;
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    super.onTaskRemoved(rootIntent);
    Log.d(TAG, "onTaskRemoved: ");
    mHandler.post(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(App.getInstance(), "Removed", Toast.LENGTH_SHORT).show();
            // api call too
        }

    });
    // for kitket
    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() + 1000, restartServicePI);

}

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

,并且在Manifest中,我已经添加了:

and in Manifest i have added this :

 <service
        android:name=".MyService"
        android:stopWithTask="false"
        >
    </service>

并在SplashScreen中启动service:

and started service in SplashScreen :

 Intent i = new Intent(this, MyService.class);
    this.startService(i);

服务启动良好,但是当我从后台删除应用程序时,从未调用onTaskRemoved.为什么 ?我做错了什么?

Service is started well but onTaskRemoved is never called when i am removing app from Background. Why ? what i am doing wrong?

注意:

有趣的结果是,我可以在某些设备上使用Toast,但在大多数设备上却不可以,并且所有设备都具有OS- Android 5.0

推荐答案

尝试这个

<service
            android:name=".MyService"
            android:enabled="true"
            android:stopWithTask="false" />

只需将Service.START_STICKY;替换为START_STICKY;

并且一旦从最近的应用列表中删除/清除应用,您就可以呼叫onTaskRemoved(Intent rootIntent).

And you will able to call onTaskRemoved(Intent rootIntent) once app removed/clear from recent app list.

这篇关于从不调用Android服务的onTaskRemoved!为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆