如何从Android应用程序注销。

时间:2014-03-12 11:25:06

标签: java android session

这是我的代码。但如果这运行我的应用程序从60秒关闭。但是当我再次按app icon.its打开login.how关闭应用程序与logout.?i添加这些在我的服务类。

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
}

请给我一个建议。因为我们需要在更多时间闲置时从应用程序退出。 谢谢所有

1 个答案:

答案 0 :(得分:0)

现在它的ok.i尝试使用活动类而不是服务类,我在登录屏幕页面后添加了代码。现在它对我的工作完美: - )

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