2011-05-26 60 views
3

我想問一下要使用的服務,以及如何做提醒的機器人......比方說:從現在開始10分鐘後給我看在通知欄通知...Android提醒!

謝謝您的回答

+0

寫了一個教程。通知提醒:http://blog.blundell-apps.com/notification-for-a-user-chosen-time/ – Blundell 2012-02-21 13:21:50

回答

9

顯然你應該使用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); 

其中週期是你的時間的東西,應該在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); 
} 

享受。

1

Somethink like this我猜你在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);