画面に赤い四角形を配置し、次のような onDestroy() を持つサービスがあります
@Override
public void onDestroy() {
Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
}
私の活動の1つから、このサービスを次のように強制終了するために呼び出しています:
stopService(new Intent(this, assNavOverrideService.class));
onDestroy() が呼び出されたことを示すトーストが表示されますが、まだ赤い四角形が表示され、まだ実行中です..
私のマニフェストは使用しています:
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" >
マイサービス
public class assNavOverrideService extends Service {
Timer timer;
WindowManager.LayoutParams handleParams;
View screenBlock;
View disableStatusBar;
WindowManager winMgr;
@Override
public void onCreate() {
winMgr = (WindowManager)getSystemService(WINDOW_SERVICE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
disableStatusBar = new TableLayout(getApplicationContext());
handleParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FILL_PARENT,
50,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
// this is to keep button presses going to the background window
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSPARENT);
handleParams.gravity = Gravity.TOP;
handleParams.y = 750;
disableStatusBar.setBackgroundResource(R.drawable.fake_nav);
winMgr.addView(disableStatusBar, handleParams);
timer = new Timer();
uiCheckTask tTask = new uiCheckTask();
tTask.setService(this);
timer.schedule(tTask, 50, 10);
// If we get killed, after returning from here, restart
return START_REDELIVER_INTENT;
}
@Override
public IBinder onBind(Intent intent) {
// We don't provide binding, so return null
return null;
}
@Override
public void onDestroy() {
Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
}
class uiCheckTask extends TimerTask {
Boolean secDialog = false;
assNavOverrideService myService;
public void setService(assNavOverrideService mS){
myService = mS;
}
public void run() {
Log.v("TICK","TOCKKKKKKKKKKK");
}
}
}