【问题标题】:Which function is called when application is removed from task manager从任务管理器中删除应用程序时调用哪个函数
【发布时间】:2016-03-01 17:39:29
【问题描述】:

我需要使用户处于离线状态。当我按下主页按钮 onStop() 时,这很好。当我按下返回按钮时,onDestroy() 被调用。但是当我通过滑动从最近的应用中关闭应用时,onStop()onDestroy() 不会被调用。

我需要知道应用何时从最近的应用中关闭以执行某些操作(例如,使用户离线)。

【问题讨论】:

    标签: android android-activity android-lifecycle


    【解决方案1】:
    1. 提供服务:

      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();
      }
      }
      
    2. 制作一个自定义类:

      public class DefaultBinder extends Binder {
          MyService s;
      
          public DefaultBinder( MyService s) {
              this.s = s;
          }
      
          public MyService getService() {
              return s;
          }
      }
      
    3. 添加到您的活动中:

      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) {}
          }
      }
      

    【讨论】:

      【解决方案2】:

      但是当我通过滑动从最近的应用中关闭应用时,不会调用 onStop() 或 onDestroy()。

      Activity 不再可见时调用的Activity lifecycle 方法不能保证在从最近的任务中删除时被调用(将其视为由系统由于内存不足)。

      我需要知道应用何时从最近的应用中关闭以执行某项操作(例如让用户离线)

      我建议以下之一:

      • 如果适用)使用ActivityonResume()/onPause()“使用户在线/离线”
      • 在应用程序中使用Servicesticks 意味着如果应用程序在ServiceonStartCommand() 返回后被杀死,将重新创建服务并再次调用onStartCommand()。此时您可以“使用户离线”。生命周期方法调用链将是:

        1. Activity's onStop() -> onDestroy()* ->
        2. ServiceonTaskRemoved()* ->
        3. Application's onCreate() -> Service's onCreate() ->
        4. Service's onStartCommand()

      传递给方法的Intent将帮助您识别哪个组件触发了启动请求:

      • Intent != null,表示已从正在运行的 Activity 实例收到请求
      • Intent = null,表示请求已由(新创建的)Application 实例发送

      * 不保证会被调用

      【讨论】:

      • 不,@Onik,您甚至不能使用粘性服务,因为该服务将被 Android 操作系统残酷终止
      • @Attiq ur Rehman ...然后将重新创建服务。见START_STICKY
      【解决方案3】:

      不,没有干净的方法来获取应用程序终止时间。但我可以建议你一个肮脏的技巧,使用服务每隔 n 分钟更新一次应用程序(离线功能)。

      当操作系统杀死你的应用程序时,它会删除所有相关的服务和广播接收器。

      【讨论】:

        猜你喜欢
        • 2014-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-30
        相关资源
        最近更新 更多