2010-07-02 39 views
18

Tôi đang cố gọi phương thức stopService() của lớp dịch vụ từ hoạt động của mình. Nhưng tôi không biết cách truy cập phương thức stopervice từ lớp hoạt động của mình. Tôi có đoạn code dưới đây nhưng nó không làm việc ...........Cách gọi phương thức stopervice() của lớp Dịch vụ từ lớp hoạt động gọi

Đây là màn hình chính lớp:

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); 

    } 


} 

Mọi đề xuất đều đánh giá cao. Cảm ơn và kính trọng Mintu

+0

http://stackoverflow.com/questions/5555765/stop-service-in-android liên quan –

Trả lời

3

Có vẻ như nó sẽ dừng dịch vụ khi bạn bỏ chọn hộp kiểm. Có ngoại lệ nào trong nhật ký không? stopService trả về boolean cho biết liệu nó có thể ngừng dịch vụ hay không.

Nếu bạn đang bắt đầu dịch vụ của mình theo Mục đích, bạn có thể muốn mở rộng IntentService thay vì Dịch vụ. Lớp đó sẽ tự dừng dịch vụ khi nó không còn hoạt động nữa.

dịch vụ ô tô

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

Tôi thực sự sử dụng khá nhiều mã giống như bạn ở trên. đăng ký dịch vụ của tôi trong biểu hiện như sau

<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> 

Trong lớp dịch vụ tôi đã tạo ra một chuỗi liên tục theo xác định tên dịch vụ như:

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

và từ Hoạt động theo tôi gọi nó với

startService(new Intent(MyService.MY_SERVICE)); 

và ngăn chặn nó với

stopService(new Intent(MyService.MY_SERVICE)); 

Nó hoạt động hoàn hảo. Hãy thử để kiểm tra cấu hình của bạn và nếu bạn không tìm thấy bất cứ điều gì lạ cố gắng để gỡ lỗi cho dù stopService của bạn được gọi là đúng.

+0

afaik nó 'bây giờ là một nguy cơ bảo mật để bắt đầu và dừng một dịch vụ theo cách này http://stackoverflow.com/questions/27842430/service-intent-must-be-clear-intent –

4

@Juri

Nếu bạn thêm IntentFilters cho dịch vụ của bạn, bạn đang nói bạn muốn để lộ dịch vụ của bạn đến các ứng dụng khác, sau đó nó có thể được ngừng đột ngột bởi các ứng dụng khác.

16

Trong thực tế để dừng dịch vụ chúng ta phải sử dụng phương pháp stopService() và bạn đang làm trong đúng cách:

Bắt đầu dịch vụ:

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

Dừng dịch vụ:

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

nếu bạn gọi stopService(), sau đó phương pháp onDestroy() trong dịch vụ này được gọi là (KHÔNG phải stopService() phương pháp):

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

bạn phải thực hiện các phương pháp onDestroy() !.

Đây là số complete example bao gồm cách bắt đầu/dừng dịch vụ.

Các vấn đề liên quan