2014-03-12 84 views
1

這裏是我的code.but如果這運行我的應用程序關閉60秒。但是當我再次按應用程序icon.its打開爲logged.how關閉應用程序與註銷。?我添加這些在我的服務類。如何從Android應用程序註銷。

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

你在哪裏存儲登錄憑據? –

+1

您可能需要重寫onPause或onResume方法在您的活動中並註銷其中一個 – Athanor

+0

@Siddharth Vyas,我還沒有得到您的觀點親愛的朋友 –

回答

0

現在它ok.i試圖與活動類,而不是服務類和我的登錄界面後添加代碼去爲我page.now其工作perrfect :-)

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 
     } 
    } 
相關問題