Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当应用程序从堆栈中清除时,即当应用程序在android中进入销毁状态时,如何注销API_Android_Session - Fatal编程技术网

当应用程序从堆栈中清除时,即当应用程序在android中进入销毁状态时,如何注销API

当应用程序从堆栈中清除时,即当应用程序在android中进入销毁状态时,如何注销API,android,session,Android,Session,我正在开发银行应用程序,我想在5分钟后当用户处于非活动状态时自动编译应用程序并销毁应用程序 我正在为前台服务使用计时器,5分钟后它将进入登录屏幕 如果不单击注销选项,用户将清除应用程序,这意味着销毁应用程序。自动登录将如何工作。给我一个解决方案 这段代码是我在“公共活动中的OnUserInteract方法”中调用的 公共类MyApp1扩展应用程序{ 私人登录Listener1监听器; 私人定时器; 私人语境; 公共无效startUserSession(上下文ctx){ this.context=

我正在开发银行应用程序,我想在5分钟后当用户处于非活动状态时自动编译应用程序并销毁应用程序

我正在为前台服务使用计时器,5分钟后它将进入登录屏幕

如果不单击注销选项,用户将清除应用程序,这意味着销毁应用程序。自动登录将如何工作。给我一个解决方案

这段代码是我在“公共活动中的OnUserInteract方法”中调用的

公共类MyApp1扩展应用程序{
私人登录Listener1监听器;
私人定时器;
私人语境;
公共无效startUserSession(上下文ctx){
this.context=ctx;
long sessiontime=Prefs.getsessiontime(ctx);
最终长毫秒=会话时间*60000;
取消计时器();
定时器=新定时器();
timer.schedule(新TimerTask(){
@凌驾
公开募捐{
试一试{
布尔前景=新建ForegroundCheckTask().execute(context.get();
如果(前景){
onSessionLogout();
}否则{
long millis=新日期().getTime();
Prefs.setcurrenttimestamp(上下文,毫秒);
}
}捕获(InterruptedException | ExecutionException被忽略){
}
}
}毫秒);
}
公共无效取消计时器(){
if(timer!=null)timer.cancel();
}
公共无效registerSessionListener(LogOutListener1 listener){
this.listener=listener;
}
公共空间无效{
startUserSession(上下文);
}
私有静态类ForegroundCheckTask扩展了AsyncTask{
@凌驾
受保护的布尔doInBackground(上下文…参数){
最终上下文=参数[0]。getApplicationContext();
返回isAppOnForeground(上下文);
}
专用布尔值isAppOnForeground(上下文){
ActivityManager ActivityManager=(ActivityManager)context.getSystemService(context.ACTIVITY_服务);
List appProcesses=activityManager.getRunningAppProcesses();
if(appprocesss==null){
返回false;
}
最后一个字符串packageName=context.getPackageName();
对于(ActivityManager.RunningAppProcessInfo appProcess:AppProcesss){
if(appProcess.importance==ActivityManager.RunningAppProcessInfo.importance\u前台和&appProcess.processName.equals(packageName)){
返回true;
}
}
返回false;
}
}
}
也许这对你有帮助

根据谷歌新的
LifecycleObserver
机制将帮助您

首先,在应用程序的项目中添加以下两个依赖项
首先,添加项目级
gradle

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://maven.google.com' }
    }
}
然后在应用程序级别
gradle

implementation "android.arch.lifecycle:extensions:1.1.0"
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
然后在创建应用程序类并添加下面的逻辑之后(注意:如果已经创建了,那么修改并添加下面的一些方法)

最后,若您还并没有在清单中注册这个类,那个么请注册以在清单中设置名称标记

<application
        android:name=".MyApplication"

如果在用户登录时保存,请清除用户数据。注销值也应在数据库中更新,当用户直接销毁应用程序时,我想调用API。您可以检查应用程序生命周期,以决定当用户销毁应用程序时应执行的操作。我在活动中调用此应用程序类,因此ondestroy方法将在应用程序被终止时调用,在该位置,我调用注销API,但不调用它。不管有没有其他方法。请建议我你现在的方法也很好。我也想调用注销API,在数据库中的值应该是更改的。如果应用程序在删除登录后打开(我们在OnApp ForeGrounded中进行了此操作),您可以调用注销API管理一个标志。您可以在splash登录中设置更多标志并处理调用前一个会话注销可能是。我只是这样做,但这种方式是错误的,对吗,当应用程序自动销毁时,应该注销它。在oreo版本中,服务工作不正常,taskfromremoved方法未调用
public class MyApplication extends Application implements LifecycleObserver {


    String strPrafKey = "bi_vrnfX";
    String strKeyTime = "timeKey";
    private String strKeyLogin = "is_login";

    @Override
    public void onCreate() {
        super.onCreate();
        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onAppBackgrounded() {
        //App in background

        //Store app goes in background time
        SharedPreferences sharedpreferences = getSharedPreferences(strPrafKey, Context.MODE_PRIVATE);
        long currentTimeInSecond = Calendar.getInstance().getTimeInMillis() / 1000;
        sharedpreferences.edit().putLong(strKeyTime, currentTimeInSecond).apply();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onAppForegrounded() {
        // App in foreground

        SharedPreferences sharedpreferences = getSharedPreferences(strPrafKey, Context.MODE_PRIVATE);
        long lastBackgroundTime = sharedpreferences.getLong(strKeyTime, 0); //Take last background time for compare
        long currentTimeInSecond = Calendar.getInstance().getTimeInMillis() / 1000;
        long timeDeffrance = (currentTimeInSecond - lastBackgroundTime) / 60; //Diffrance of last background and current time in minute
        if (timeDeffrance > 5) {
            sharedpreferences.edit().putBoolean(strKeyLogin, false).apply();
            //You can replace this code or data with which you are comparing in your login
        }
    }
}
<application
        android:name=".MyApplication"