2012-11-05 64 views
0

我有一個應用程序,它包含兩個活動和一個服務和一個接收器,服務用於始終獲取位置更新,我將在第一次啓動接收器中的服務,我的要求是,當我的應用程序處於前臺(運行)時,我需要停止服務,並且我需要在應用程序停止時啓動該服務。在兩個活動中,initActivity是應用程序啓動時啓動的第一個活動,homegridActivity id是第二個活動,我在homegridactivity中提供了退出應用程序的選項。下面是我開始服務和停止服務的代碼。明確提供服務

class initActivity extends Activity { 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.app); 
    System.out.println("VANDROID service status inside oncreate of init"); 
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { 
     if ("com.vaayoo.android.service.VLocationFetchService".equals(service.service.getClassName())) { 
      Intent intent = new Intent(VUiHelper.getInstance().getApplicationContext(), VLocationFetchService.class); 
      boolean result = ((Vandroid)VUiHelper.getInstance().getApplicationContext()).stopService(intent); 
      System.out.println("VANDROID service status" + result); 
      manager = null; 
     } 

    } 

} 

} //第二活動

Class HomeGridActivity extends Activity 
{ 
protected void onDestroy() { 
    super.onDestroy(); 
    System.out.println("Homegrid activity onDestroy called"); 
    VUiHelper.getInstance().clearControlCache(); 
    ActivityManager manager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); 
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { 
     if ("com.vaayoo.android.service.VLocationFetchService".equals(service.service.getClassName())) { 
      Intent intent = new Intent(VUiHelper.getInstance().getApplicationContext(), VLocationFetchService.class); 
     ComponentName compname =((Vandroid) VUiHelper.getInstance().getApplicationContext()).startService(intent); 
     System.out.println("COMPONENT NAME" + compname.toString()); 
      manager = null; 
     } 
    } 
} 

}

我回採服務中的OnCreate上initactivity和homegridActivity的開始的OnDestroy相同的服務,我面臨的問題是,這只是第一次工作,如果我多次關閉並啓動應用程序,服務沒有停止和啓動,我發現服務一直在運行。我已經將該服務作爲start_sticky進行創建,以便在任何時候不應該被android運行時殺死,我應該完全控制該服務以停止並啓動。無法找出爲什麼它不能一直工作,爲什麼它只能第一次工作。我做錯了什麼?如何解決這個問題?是否因爲將服務作爲start_sticky?高度讚賞任何形式的幫助..在此先感謝..

+0

FYI「我需要停止服務」的滋生,你必須做一些奇怪的事情開始與 –

+0

想法你有沒有試圖把'startService'代碼'的onResume()'和'的代碼stopService'在'onPause()'? – varevarao

+0

我不是在onResume()中停止它並在onPause(),bcoz中停止它,我知道oncreate將在任何成本時被調用,當一個活動啓動時,並在銷燬時,活動停止。我已經把日誌和sop的,並監視,每次活動啓動和關閉時,我得到這些日誌和sop的。我只擔心,爲什麼它是第一次工作,爲什麼不在多次? – RockandRoll

回答

2

此方法返回true或false means.when服務是ruuning比reture真實,否則爲false。所以你可以使用這種方法,當你使用就像這樣。

private boolean isMyServiceRunning() { 
     ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 
     for (RunningServiceInfo service : manager 
       .getRunningServices(Integer.MAX_VALUE)) { 
      if ("packagename".equals(service.service 
        .getClassName())) { 
       return true; 
      } 
     } 
     return false; 
    } 

並檢查那樣。

if (isMyServiceRunning()) { 
    stopService(new Intent(A.this, MusicService.class)); 

    } 
+0

我試過這個,但沒有工作..我想知道,它爲什麼只是第一次工作,而不是多次? – RockandRoll

+0

我認爲你檢查OnCreate .. so – ckpatel