2012-09-01 33 views
-1

其實我已經進行了一些技術,其中一些特定的方法將會在指定的時間,直到應用destroyed.I谷歌之後被調用,並發現services.Using服務,這項工作可以performed.I也水漲船高通過許多服務教程,所以現在我可以使用服務,但任何人都可以告訴我如何做到這一點。我應該用呼叫特定的粘性服務於指定的時間後活動的背景...謝謝....運行Android服務,直到應用程序銷燬

編輯:我用下面的代碼

public class MyService extends Service { 

    int count = 0; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     super.onStart(intent, startId); 
     handler.removeCallbacks(updateTimeTask); 
     handler.postDelayed(updateTimeTask, 1000); 
     Toast.makeText(this, "Service started................", 
       Toast.LENGTH_SHORT).show(); 
    } 

    private Runnable updateTimeTask = new Runnable() { 
     public void run() { 
      if (count == 50) { 
       count = 0; 
       Tocken_parser tocken = new Tocken_parser(); 
       tocken.tockenParser("1"); 
       Toast.makeText(MyService.this, "coutn===", Toast.LENGTH_SHORT) 
         .show(); 
       System.out.println("Count ==============="); 
      } 
      count++; 
      handler.postDelayed(this, 1000); 
     } 

    }; 
    private Handler handler = new Handler(); 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     System.out 
       .println("Service destroyed........................................"); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 
} 

其工作正常,但告訴我一件事,當活動被破壞時如何停止服務。

回答

0

答案是YES。此外,由Activity產生的Service將一起銷燬;除非,您指定Alarm的計劃檢查來喚醒您的死亡Service

+0

感謝@xjaphx我已經編輯我的問題有外觀和請告訴我如何停止服務。 – Dilip

相關問題