Androidで作成したサービスを終了できませんでしたが、stopServiceの後も実行されたままです。次のようなサービスを開始:サービスを正しく終了するには、stopService()が機能していませんか?
Intent i = new Intent(this, MyService.class);
startService(i);
関連マニフェストエントリ:
<service android:name=".services.MyService" ></service>
いくつかの活動全体に持続しますので、私はIntentServiceを使用しなかったたMyServiceの要旨:
package com.sample.app.services;
public class MyService extends Service {
public static final String TAG = MyService.class.getSimpleName();
private Handler handler;
protected void onHandleIntent(Intent intent) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getBaseContext(), "MyService Intent... Start", Toast.LENGTH_LONG).show();
}
});
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handler = new Handler();
myProcessing();
// This will cause the service to hang around
return (START_STICKY);
}
@Override
public IBinder onBind(Intent intent) {
return (null);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
super.onDestroy()の前に、上記のonDestroy()でさまざまなアクティビティを試してみました。私は以下を試しました:
Intent i = new Intent(this, MyService.class);
stopService(i);
MyServiceはただ実行し続けます。定義上、このサービスはシングルトンだと私は信じています。
私はonDestroyを実装し、このイベント内のすべてのワーカースレッドを停止する必要があると思います! –
試しonDestroy()まず、運がない。 –