gpt4 book ai didi

java - 如何从 Android 应用程序注销?

转载 作者:太空宇宙 更新时间:2023-11-04 15:03:01 24 4
gpt4 key购买 nike

这是我的代码。但是如果这运行我的应用程序从 60 秒开始关闭。但是当我再次按下应用程序图标时。它以登录状态打开。如何通过注销关闭应用程序。?我将这些添加到我的服务类中。

public class BackgroundService extends Service {
private int interval3 = 10; // 10 seconds

private Handler mTimer3 = new Handler();
private Runnable mTask3 = new Runnable() {
public void run() {
mTimer3.postDelayed(this, interval3 * 1000L);
CountDownTimer timer = new CountDownTimer(10*1000, 1000) {

public void onTick(long millisUntilFinished) {
Log.w("Seconds remaining: ", String.valueOf(millisUntilFinished / 1000));
}

public void onFinish() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
};
timer.start();
}
};

public void onCreate() {
mTimer1.postDelayed(mTask1, interval1 * 1000L); // start the timer for the first time
mTimer2.postDelayed(mTask2, interval2 * 1000L); // start the timer for the first time
mTimer3.postDelayed(mTask3, interval3 * 1000L); // start the timer for the first time
}

请给我一个建议。因为我们需要在更多时间空闲时从应用程序中退出。谢谢大家

最佳答案

现在可以了。我尝试使用 Activity 类而不是服务类,并且我在登录屏幕后的页面中添加了代码。现在它对我来说非常完美:-)

public class #MyActivityClass extends Activity {


//=============================================================================================================================
private Handler mTimer3 = new Handler();
private Runnable mTask3 = new Runnable() {
public void run() {
CountDownTimer timer = new CountDownTimer(10*1000, 1000) {

public void onTick(long millisUntilFinished) {
Log.w("Seconds remaining: ", String.valueOf(millisUntilFinished / 1000));
}

public void onFinish() {
//Here finally added finish(); and System.exit(0); methods
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//***Change Here***
startActivity(intent);
finish();
System.exit(0);
}
};
timer.start();
mTimer3.postDelayed(this, interval3 * 1000L);
}
};

private int interval3 = 10*60; // 60 seconds

@Override
protected void onDestroy() {
if (mTimer3 != null) {
mTimer3.removeCallbacks(mTask3); // cancel the timer
}
}

关于java - 如何从 Android 应用程序注销?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22350012/

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