2016-05-31 87 views
0

我在我的應用程序中使用服務,但我通過從中獲取對象來運行它,因爲我將活動上下文傳遞給它,所以我想知道如何從活動中停止此服務從活動中停止服務

我的服務

public class Tracker extends Service implements LocationListener 

我如何啓動它

T1=new Tracker(MainActivity.this); 

我試圖

​​

也是我的服務定義

public void Close() 
{ 
    this.stopSelf(); 
} 

,並把它稱爲從主

T1.Close(); 

但它仍在運行

回答

0

要啓動服務,你可以這樣做:

Intent intent = new Intent(this, Tracker.class); 
startService(intent); 
要停止它:
Intent intent = new Intent(this, Tracker.class); 
stopService(intent); 

希望它有幫助。