从任务管理器中删除应用程序时调用哪个函数 [英] Which function is called when application is removed from task manager

查看:86
本文介绍了从任务管理器中删除应用程序时调用哪个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用户状态为脱机.当我按下主页按钮onStop()时,没关系.当我按下后退按钮onDestroy()被调用.但是,当我通过滑动从最近的应用程序中关闭该应用程序时,不会调用onStop()onDestroy().

I need to make status of user offline. When I press home button onStop() is called, that's fine. When I press back button onDestroy() is invoked. But when I close the app from recent apps by swiping it, onStop() or onDestroy() isn't called.

我需要知道何时从最近的应用程序中关闭该应用程序以执行某些操作(例如,使用户脱机).

I need to know when the app is closed from recent apps to do something (e.g make user offline).

推荐答案

  1. 提供服务:

  1. Make a service :

public class MyService extends Service {
private DefaultBinder mBinder;
private AlarmManager  alarmManager ;
private PendingIntent alarmIntent;

private void setAlarmIntent(PendingIntent alarmIntent){
this.alarmIntent=alarmIntent;
}

public void onCreate() {
alarmManager (AlarmManager)getSystemService(Context.ALARM_SERVICE);
mBinder = new DefaultBinder(this);
}

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

 public void onTaskRemoved (Intent rootIntent){
 alarmManager.cancel(alarmIntent);
 this.stopSelf();
}
}

  • 创建自定义类:

  • Make a custom class :

    public class DefaultBinder extends Binder {
        MyService s;
    
        public DefaultBinder( MyService s) {
            this.s = s;
        }
    
        public MyService getService() {
            return s;
        }
    }
    

  • 添加到您的活动中:

  • Add to your activity :

    MyService service;
    protected ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder binder) {
    service = ((DefaultBinder) binder).getService();
    service.setAlarmIntent(pIntent);
        }
    
        public void onServiceDisconnected(ComponentName className) {
            service = null;
        }
            };
    
    protected void onResume() {
        super.onResume();
        bindService(new Intent(this, MainService.class), mConnection,
                Context.BIND_AUTO_CREATE);
    }
    
    @Override
    protected void onStop() {
        super.onStop();
    
        if (mConnection != null) {
            try {
                unbindService(mConnection);
            } catch (Exception e) {}
        }
    }
    

  • 这篇关于从任务管理器中删除应用程序时调用哪个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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