我需要在我的Android应用程序中实现自动注销功能,无论当前显示哪个屏幕。
提前致谢!
答案 0 :(得分:2)
只需在login
之后设置一个警报管理器,然后只要该警报触发,然后调用一个broadcast reciver
并在broadcast
中只编写注销代码和clear all session value
相关的警告到login
你可以像这样重启你的应用程序,从这个所有预览活动中,所有顶部也将清晰
Intent i = cntxt.getPackageManager().getLaunchIntentForPackage(cntxt.getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addCategory(Intent.CATEGORY_HOME);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
cntxt.startActivity(i);
答案 1 :(得分:2)
在您的情况下,我会在其中创建Service然后AlarmManager
。
public class AutoLogOutService extends Service {
@Override
public void onCreate() {
super.onCreate();
setAlarm(0, 60 * 60 * 1000);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent == null) {
return START_STICKY;
}
final String intentAction = intent.getAction();
if ("action_log_out".equals(intentAction)) {
logOut();
}
return START_STICKY;
}
public void setAlarm(long startAt, long ulogOutPeriod) {
final Context context = getBaseContext();
Intent intent = new Intent(context, AutoLogOutService.class);
intent.setAction("action_log_out");
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(
AlarmManager.RTC,
startAt,
updatePeriod,
pendingIntent);
}
}
同样要通过设备启动启动它,我会注册启动BroadcastReceiver
:
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, PlanetsService.class);
context.startService(startServiceIntent);
}
}
在你的清单中:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name="yourpackage.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
通常会话数据存储在SharedPreferences
中。我写了一篇关于它们的文章,你可以找到它here
答案 2 :(得分:0)
自动会话过期:
使用代表您的令牌的静态数据成员及其验证时间。检查每个活动的onResume()中的标记。如果令牌丢失或已过期,请将用户重定向到登录活动。
手动退出:
public void logout(View view){
SharedPreferences sharedpreferences = getSharedPreferences
(MainActivity.MyPREFERENCES, Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.clear();
editor.commit();
moveTaskToBack(true);
Welcome.this.finish();
以下是您的教程指南
http://www.tutorialspoint.com/android/android_session_management.htm