2010-07-02 77 views
18

我想從我的活動中調用服務類的stopService()方法。 但我不知道如何從我的活動類訪問stopservice方法。 我有下面的代碼,但它不工作...........如何從調用活動類中調用服務類的stopservice()方法

這是主屏幕類:

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     enablecheck = (CheckBox)findViewById(R.id.enablecheck);    

     enablecheck.setOnCheckedChangeListener(new OnCheckedChangeListener() 
     { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       // TODO Auto-generated method stub 
       if(enablecheck.isChecked()){  
        startService(new Intent(HomeScreen.this, AutoService.class)); 
       }else 
       { 
        stopService(new Intent(HomeScreen.this, AutoService.class)); 

       }      
      } 

     }); 

    } 
This is Service Class: 
public class AutoService extends Service { 

    private static final String TAG = "AutoService"; 
    private Timer timer;  
    private TimerTask task; 

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

    @Override 
    public void onCreate() { 
     Toast.makeText(this, "Auto Service Created", Toast.LENGTH_LONG).show(); 
     Log.d(TAG, "onCreate");  

     int delay = 5000; // delay for 5 sec. 
     int period = 5000; // repeat every sec. 

     timer = new Timer(); 

     timer.scheduleAtFixedRate(task = new TimerTask(){ 
      public void run() 
      { 
       System.out.println("done"); 
      } 
     }, delay, period); 


    } 


    @Override 
    public boolean stopService(Intent name) { 
     // TODO Auto-generated method stub 
     timer.cancel(); 
     task.cancel(); 
     return super.stopService(name); 

    } 


} 

任何建議非常可觀。 感謝和問候 明圖

+0

相關http://stackoverflow.com/questions/5555765/stop-service-in-android – 2016-02-14 10:26:26

回答

3

看起來應該停止服務,當您取消複選框。日誌中是否有例外情況? stopService返回一個布爾值,指示是否能夠停止該服務。

如果您是通過Intents開始您的服務,那麼您可能需要擴展IntentService而不是Service。當沒有其他工作要做時,這個班會自行停止服務。

汽車維修

class AutoService extends IntentService { 
    private static final String TAG = "AutoService"; 
    private Timer timer;  
    private TimerTask task; 

    public onCreate() { 
      timer = new Timer(); 
      timer = new TimerTask() { 
      public void run() 
      { 
       System.out.println("done"); 
      } 
      } 
    } 

    protected void onHandleIntent(Intent i) { 
     Log.d(TAG, "onHandleIntent");  

     int delay = 5000; // delay for 5 sec. 
     int period = 5000; // repeat every sec. 


     timer.scheduleAtFixedRate(timerTask, delay, period); 
    } 

    public boolean stopService(Intent name) { 
     // TODO Auto-generated method stub 
     timer.cancel(); 
     task.cancel(); 
     return super.stopService(name); 
    } 

} 
28

我實際使用幾乎相同的代碼,你在上面。在清單中我的服務註冊是以下

<service android:name=".service.MyService" android:enabled="true"> 
      <intent-filter android:label="@string/menuItemStartService" > 
       <action android:name="it.unibz.bluedroid.bluetooth.service.MY_SERVICE"/> 
      </intent-filter> 
     </service> 

在服務I類創建了一個根據恆定字符串,標識等的服務名稱:

public class MyService extends ForeGroundService { 
    public static final String MY_SERVICE = "it.unibz.bluedroid.bluetooth.service.MY_SERVICE"; 
    ... 
} 

,並從根據活性我與

調用它
startService(new Intent(MyService.MY_SERVICE)); 

stopService(new Intent(MyService.MY_SERVICE)); 
停止

它完美的工作。嘗試檢查你的配置,如果你沒有發現任何奇怪的東西,試着調試你的stopService get是否被正確調用。

+0

afaik它現在是一個安全風險,啓動和停止服務這種方式http://stackoverflow.com/questions/27842430 /服務意向必須待明確意圖 – 2016-06-17 11:26:22

4

@Juri

如果添加IntentFilters爲你服務,你說你要暴露你的服務,其他應用程序,則可以通過其他應用程序意外停止。

16

其實到停止服務我們必須使用方法stopService(),你在正確的方式是這樣做的:

開始服務:

Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class); 
    startService(myService); 

停止服務:

Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class); 
    stopService(myService); 

if你叫stopService(),則該方法onDestroy()在被稱爲服務(不是該stopService()方法):

@Override 
    public void onDestroy() { 
     timer.cancel(); 
     task.cancel();  
     Log.i(TAG, "onCreate() , service stopped..."); 
    } 

您必須實現onDestroy()方法!

這裏是一個complete example包括如何啓動/停止服務。