簡體   English   中英

Android 提醒!

[英]Android reminder!

我想問在android中使用哪個服務以及如何使用提醒...假設:10分鍾后在通知欄中顯示通知...

感謝您的回答

顯然,您應該使用 AlarmManager 來設置要在給定 PERIOD 中執行的內容。

AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, OnAlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
SystemClock.elapsedRealtime(), PERIOD, pi);

其中 PERIOD 是您應該在 OnAlarmReceiver 中執行的操作的時間。 然后,只需在

@Override
public void onReceive(Context context, Intent intent) {
    NotificationManager nm = (NotificationManager);
    context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification();
    notification.tickerText = "10 Minutes past";
    nm.notify(0, notification);
}

享受。

您應該使用AlarmManager 有了它,您可以安排要交付的意圖。 創建一個BroadcastReceiver以獲取它並顯示通知。

我猜想像這樣,你在 10 分鍾后啟動一個 runnable 並在 runnable 的代碼中打開一個通知。

Runnable reminder = new Runnable()
{
    public void run()
    {
        int NOTIFICATION_ID = 1324;
        NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Notification note = new Notification(R.drawable.icon, "message", System.currentTimeMillis());

        // Add and AUTO_CANCEL flag to the notification, 
        // this automatically removes the notification when the user presses it
        note.flags |= Notification.FLAG_AUTO_CANCEL;    

        PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(this, ActivityToOpen.class), 0);

        note.setLatestEventInfo(this, "message", title, i);
        notifManager.notify(NOTIFICATION_ID, note);
    }
};

Handler handler = new Handler();
handler.postDelayed(reminder, 600000);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM