2012-03-02 37 views
0

我有一個服務(「Aservice」)被宣佈表現爲無法停止服務在不同的進程中運行(但在相同的應用程序)

<service android:name=".Aservice" android:process=":my_process" > </service> 

,我想知道如何從停止此服務另一項服務(讓'Bservice')的應用程序。我曾試圖這樣,服務「Aservice」查找使用

int pid = android.os.Process.myPid(); 

它的進程ID,然後發送它的ID號到另一個服務(「Bservice」)特別指定停止本身(服務Aservice),在「 Bservice」調用此代碼

android.os.Process.killProcess(pid); 

在哪裏‘的pid’是被sended的Aservice服務進程ID,但‘Bservice’無法停止服務‘Aservice’,我可以看到在手機設置 - 列出的服務>運行服務。此代碼

ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); 
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) 
    { 
     sClassName = service.service.getClassName();   
     if (sClassName.contains("com.pkg.Rand.Aservice")) 
     { return true; }   
    }  
    return false; 

返回True。它需要將服務作爲單獨的進程運行,以防止應用程序在感知位置時放慢速度。請提供有關如何在不同流程中將服務作爲獨立流程與其他服務停止運行的建議。

任何幫助,高度讚賞。

回答

3

您使用Context.startService()開始了您的服務,因此您可以使用Context.stopService()來停止它。不管它運行什麼進程都沒有關係。

相關問題