ユーザーに 12 時間ごとに通知メッセージを送信したいのですが、どこから始めればよいかわかりません。ユーザーに簡単な通知を追加するための段階的なガイドを誰か提供してもらえますか? 前もって感謝します
2 に答える
1
手順は次のとおりです。
- AlarmManagerを介してアラームを設定します
- BroadcastReceiver of alarm fireで通知を入力すると、12時間以内に次のアラームを設定できます
もう1つの方法は、最初から12時間ごとに繰り返し作成することです。
于 2013-03-14T22:31:38.147 に答える
0
このコードを使用できます:
private static final int TIME = 1000*60*60*12;
new Timer().scheduleAtFixedRate(new TimerTask() {
public void run() {
showNotification();
}
}, 0, TIME);//start immediatly, run every 12hours
public void showNotification() {
final NotificationManager mNotification = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final Intent launchNotifiactionIntent = new Intent(this, ActivityLauchedOnClickNotif.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(this,
REQUEST_CODE, launchNotifiactionIntent,
PendingIntent.FLAG_ONE_SHOT);
Notification.Builder builder = new Notification.Builder(this)
.setWhen(System.currentTimeMillis())
.setContentTitle(titleString)
.setContentText(messageString)
.setContentIntent(pendingIntent);
mNotification.notify(NOTIFICATION_ID, builder.build());
}
このコードは API 11 以降で機能します。(通知ビルダー)
于 2013-03-14T22:39:08.813 に答える