【问题标题】:How to logout from android application.?如何从android应用程序注销。?
【发布时间】:2014-04-16 11:57:49
【问题描述】:

这是我的代码。但是如果这运行我的应用程序从 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
}

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

【问题讨论】:

  • 您将登录凭据存储在哪里?
  • 您可能需要覆盖活动中的 onPause 或 onResume 方法并注销其中一个
  • @PriyanRockZ 我的意思是,当您单击应用程序图标并且应用程序显示已签名时,您可能会将凭据存储在某处。
  • 应用在 60 秒后关闭后,您可以删除/清除数据库。
  • 或者,您也可以在共享首选项中保持登录状态。检查 onresume 中的首选项并在需要注销时清除首选项

标签: java android session


【解决方案1】:

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

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
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 2014-04-27
    • 1970-01-01
    相关资源
    最近更新 更多