android - 当用户通过 android 中的任务滑动删除应用程序时执行注销

标签 android android-service swipe logout

当用户从当前运行的应用程序列表中删除应用程序时,我想清除用户凭据并注销用户。在这里,我正在做的是,当用户通过滑动应用列表结束应用时,其中的应用应该执行注销。但是当用户通过滑动删除应用程序时,它什么也不做。下面是我的代码。

public class MyService extends Service{

    public void onTaskRemoved(){
        Log.i("RootActivity:onTaskRemoved()","******TaskRemoved******");
        SharedPreferences pref = getSharedPreferences(getString(R.string.pref_current_user), MODE_PRIVATE);             
        SharedPreferences.Editor editor = pref.edit();
        editor.clear();     // CLEAR ALL FILEDS
        editor.commit();    // COMMIT CHANGES
        Log.i("RootActivity:onTaskRemoved()","******APP LOGGEDOUT******");
        setloginButton();   // Change logout button to login
        Log.i("RootActivity:onTaskRemoved()","******loginButton is set******");
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

最佳答案

这是因为您只使用 bindService() 绑定(bind)了服务,所以 onTaskRemoved() 永远不会被调用。

使用服务您可以 -

  • 绑定(bind)
  • 开始
  • 绑定(bind)并启动

documentation通常分别讨论这两种类型的服务,您的服务可以两种方式工作,这就是您需要做的 —

它可以启动(无限期运行)并且还允许绑定(bind)。

这只是您是否实现几个回调方法的问题:onStartCommand() 允许组件启动它,onBind() 允许绑定(bind)。

两者兼顾 -

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

// Bind to the service
bindService(new Intent(context, MyService.class),
mConnection, Context.BIND_AUTO_CREATE);

关于android - 当用户通过 android 中的任务滑动删除应用程序时执行注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26053784/

相关文章:

Android webview 只能放大

android - 我如何在全屏中滑动选定的网格图像

android - 除了滑动手势外,如何在 SurfaceView 上同时进行单点触摸?

android - unity C# 设备运动方向捕捉

android - 如何将焦点设置为 0 索引的 ListView

android - Oreo 无限后台服务,如 Messenger 或 Instagram

android - 为什么 Android Service 被制作成另一个应用程序组件?

jquery - 如何检测 jQuery Mobile 中的滑动位置

javascript - 如何通过同一个流发送不同编码的数据?

android - 华为 P20 Lite 杀死 Oreo 中的前台服务