gpt4 book ai didi

java - 安卓:自动注销

转载 作者:行者123 更新时间:2023-12-01 11:26:21 24 4
gpt4 key购买 nike

您好,我想在我的 Android 应用程序闲置一段时间后自动注销。在 onPause 的第二个 Activity 中,我将应用程序暂停时的时间保存在变量中。在 MainActivity 的 onresume 方法中,我将应用程序重新启动时的时间保存在另一个变量中并计算两个变量之间的差异。如果这个差值大于确定的时间,例如 10 秒就会注销。

我的 MainActivity 的代码:

public class MainActivity extends Activity {

private EditText username,password;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String name = "nameKey";
public static final String pass = "passwordKey";
SharedPreferences sharedpreferences;

public static Long t0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);

username.setText("***");
password.setText("***");

t0 = Welcome.t0;
}


@Override
protected void onResume() {
sharedpreferences=getSharedPreferences(MyPREFERENCES,
Context.MODE_PRIVATE);
if (sharedpreferences.contains(name))
{
if(sharedpreferences.contains(pass)){
Intent i = new Intent(this,SecondActivity.class);
startActivity(i);
}

Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String strDate = sdf.format(c.getTime());
System.out.println(strDate);

Date d;
try {
d = sdf.parse(strDate);
long second2=d.getSeconds();

long ts=Math.abs(second2-t0);

if (ts>=10){

logout();
Intent i2 = new Intent(this,MainActivity.class);
startActivity(i2);
}

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
super.onResume();
}

public void login(View view){
Editor editor = sharedpreferences.edit();
String u = username.getText().toString();
String p = password.getText().toString();
if(u.equals("***") && p.equals("***")){

editor.putString(name, u);
editor.putString(pass, p);
editor.commit();
Intent i = new Intent(this,SecondActivity.Welcome.class);
startActivity(i);
}
else{
Toast.makeText(MainActivity.this, "ko", Toast.LENGTH_SHORT).show();

}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


public void onPause() {
super.onPause();
this.finish();
}

public void closing() {
finish();
}

public void logout(){
SharedPreferences sharedpreferences = getSharedPreferences
(MainActivity.MyPREFERENCES, Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();

editor.clear();
editor.commit();
moveTaskToBack(true);
this.finish();
}

}

第二个Activity的代码是:

public class Second extends Activity {

static Long t0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_welcome);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.welcome, menu);
return true;
}
public void logout(View view){
SharedPreferences sharedpreferences = getSharedPreferences
(MainActivity.MyPREFERENCES, Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();

editor.clear();
editor.commit();
moveTaskToBack(true);
this.finish();
}

public void exit(View view){

moveTaskToBack(true);
this.finish();

}

public void onPause() {
super.onPause();
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String strDate = sdf.format(c.getTime());


Date d;
try {
d = sdf.parse(strDate);
long second=d.getSeconds();

Welcome.t0=second;

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

this.finish();

}

public void closing() {
finish();
}

}

这种情况不起作用,因为第一次后它工作正常,但第二次它不起作用,没有注销。我该如何解决这个问题?

最佳答案

设置闹钟:

Calendar cal = Calendar.getInstance();

cal.add(Calendar.MINUTE, "YourTimeinMinutes"); // you can use Calendar.Seconds etc according to your need

Intent intent = new Intent(YourActivity.this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(YourActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

AlarmReceiver.class

public class AlarmReceiver extends BroadcastReceiver 
{
@Override
public void onReceive(Context context, Intent intent)
{
//your logic
}
}

不要忘记在应用程序标记内的 list 中添加接收器:

  <receiver  android:name=".AlarmReceiver"  ></receiver>

添加 onPause 取消注册您的广播。

关于java - 安卓:自动注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30773640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com