2015-02-23 179 views
1

我在服務方面遇到了一些問題。服務 - 服務未停止

服務:

public class MyService extends Service { 
public MyService() { 
} 

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) { 
    timer.cancel(); 
    task.cancel(); 
    return super.stopService(name); 

} 

@Override 
public void onDestroy(){ 
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show(); 
    super.onDestroy(); 
} 

MainActivity:

public class ClientSocket extends ActionBarActivity { 

CheckBox enablecheck; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_client_socket); 

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

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

      } 
     } 

    }); 
} 

清單文件:

<?xml version="1.0" encoding="utf-8"?> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/Theme.AppCompat.Light" > 
    <activity 
     android:name=".ClientSocket" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <service 
     android:name=".MyService" 
     android:enabled="true" 
     android:exported="true" > 
    </service> 
</application> 

我只是測試,當我試圖停止服務,功能onDestroy()被調用,但不停止服務。 service/android:exported和android:enable都是真的。

任何幫助?

+0

你可以放置你的清單嗎? – 2015-02-23 22:08:45

+1

你如何斷言服務沒有停止? – Quanturium 2015-02-23 22:09:15

+0

@quanturium該程序仍然打印「完成」在日誌 – 2015-02-23 22:12:30

回答

0

Timer實例創建後臺線程。如果你想完全停止服務,你還需要停止線程。定時器有一個cancel()方法,您可以在onDestroy()方法中調用以終止定時器。

此外,導出服務意味着其他應用程序(進程)可以訪問它。你很可能不需要那個。

+1

@quanturim謝謝,我認爲stopService()的cancel()將工作。 – 2015-02-23 22:29:30

0

代碼的問題是什麼

@Override 
public boolean stopService(Intent name) { 
    timer.cancel(); 
    task.cancel(); 
    return super.stopService(name); 

} 

確實誤解。

這不是服務停止時調用的回調函數。相反,它會覆蓋Context上的stopService方法,您將稱爲停止服務(如您在示例中的活動中所做的那樣)。

實際上,在代碼中覆蓋stopService根本沒有任何效果。

timer.cancel(); 
task.cancel(); 

應該放在您的服務的方法onDestroy