常駐?
ということで作りかけの_振ったら起きる_ソレを止まらないように工作してみました。
ポイントとしては onStart における開始処理で
- Notification なオブジェクトを作る
- ContentIntent に PendingIntent を仕込む
- 仕込む PendingIntent はこの Service の onStart を呼び出す
- ACTION は既存のものと異なるものを使う
- 作った Notification なオブジェクトを使って startForeground() する
というソレ。ちなみに終了処理では
stopForeground(true);
してるのみ、です。動作確認してみた結果、どうやらこれで止まらない Service を云々することができる模様。すばら。
コード的には
以下なカンジ。微妙にバッドノウハウ感満点ですが。
private Notification.Builder make_notification() { Intent newIntent = new Intent(getApplicationContext(), ShakeWatchService.class); newIntent.setAction(ShakeWatchService.ACTION_NOTIFICATION); PendingIntent ci = PendingIntent.getService(getApplicationContext(), 0, newIntent, PendingIntent.FLAG_ONE_SHOT); return new Notification.Builder(getApplicationContext()) .setSmallIcon(R.drawable.ic_launcher) .setOngoing(true) .setContentIntent(contentIntent); } @Override public void onStart(Intent intent, int startid) { Log.d(TAG, "onStart"); if (ACTION_EXECUTE.equals(intent.getAction())) { execute_local(); notification = make_notification() .setContentTitle("Shake on Started") .setContentText("Touch for Pause Shake Screen on") .setTicker("Shake on Started") .getNotification(); startForeground(R.string.app_name, notification); } else if (ACTION_STOP.equals(intent.getAction())) { stop_local(); stopForeground(true); notification = null; }
微妙なローカルメソドを呼びだしてますがスルーで。ちなみに上記では引用スルーしてますが ACTION_NOTIFICATION な処理は onStart 内に追加しています。
残
- appWidget で Screen OFF な部品
- 設定見て起動時に Service 開始する receiver
- UI 整備
最後のがいっちゃんハードル高いし。とは言え、これ以外を盛り込んだ時点で github 方面にも push しちゃう方向です。