可以使用接收器和服務如下:
1)創建的重複頻率:
private void setRecurringAlarm(Context context) {
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getDefault());
updateTime.set(Calendar.HOUR_OF_DAY, 12);
updateTime.set(Calendar.MINUTE, 30);
Intent downloader = new Intent(context, BroadcastReceiverService.class);
downloader.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
pendingIntent = PendingIntent.getBroadcast(context, 0, downloader, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), 30 * 1000/*AlarmManager.INTERVAL_FIFTEEN_MINUTES*/, pendingIntent);
Log.d("MyActivity", "Set alarmManager.setRepeating to: " + updateTime.getTime().toLocaleString());
}
2)創建BroadcastReceiverService類
public class BroadcastReceiverService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent dailyUpdater = new Intent(context, MyService.class);
context.startService(dailyUpdater);
// context.stopS 登錄.d(「AlarmReceiver」,「從AlarmReceiver.onReceive調用context.startService」); } }
3)創建的通知時,點擊通知和相關的行動:
public class MyService extends IntentService {
public MyService() {
super("MyServiceName");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("MyService", "About to execute MyTask");
new MyTask().execute();
this.sendNotification(this);
}
private class MyTask extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... strings) {
Log.d("MyService - MyTask", "Calling doInBackground within MyTask");
return false;
}
}
private void sendNotification(Context context) {
Intent notificationIntent = new Intent(context, SupportActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
NotificationManager notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(android.R.drawable.star_on, "New Message...", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, "Message Support Title","Content Message text", contentIntent);
notificationMgr.notify(0, notification);
}
}
4)SupportActivity是將被打開一次點擊notofication任何活動
謝謝