ただし、スクリプトの「when」パラメータは、呼び出されたときにステータスバー通知が常にトリガーされるため、やや混乱を招きます。通知通知=newNotification(icon、tickerText、when);
正確に-呼び出されると通知がトリガーされます。例のようにwhen変数をSystem.currentTimeMilis()に設定すると、-今すぐ通知を表示することを意味します。通知をトリガーするものとして、それを処理するのはあなた次第です。アクティビティは良い選択ではないようですが、サービスは良い選択です。アプリケーションの開始時にサービスを初期化し(アプリケーションの終了時に停止することを忘れないでください)、通知の「リスニング」とトリガーを実行させます。次のようになります。
public class NotifyService extends Service {
private NotificationManager mNM;
@Override
public void onCreate() {
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//do some work, listen for change
if (triggerSatisfied) showNotification();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onDestroy() {
// Cancel the persistent notification.
mNM.cancelAll();
}
@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
private final IBinder mBinder = new LocalBinder();
private void showNotification() {
//code for notification goes here
}
public class LocalBinder extends Binder {
NotifyService getService() {
return NotifyService.this;
}
}