定期的にスクリーンショットを取得するサービスが中々終了しなかったので記載。
タイマー処理付きのサービスはタイマー停止後にサービスを止める
(サービスだけ止めてもタイマーは裏で動き続け、プロセスが残る)。
ここではServiceで定義したtask(timer)をcancelし、stopServiceで止めている。
サービスの起動確認をする場合、タイマーを止めなければtrueが通り続ける。
サービス名:SService
Activity.java
//タイマー処理確認
SService ssservice= new SService();
if(ssservice.task != null) {
ssservice.task.cancel();
ssservice.task = null;
}
//サービス停止
android.content.Intent _intent;
_intent = new android.content.Intent(getApplicationContext(), ssservice.class);
getApplicationContext().stopService(_intent);
以下のソースでサービスの起動確認が可能。参考にどうぞ
Activity.java
private boolean isMyServiceRunning(Class<?> serviceClass) {
//サービス起動確認
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
//サービス起動済み
return true;
}
}
//サービスが起動していない
return false;
}