這是方法來設置每日鬧鐘5時15分AlarmManager通知每天上午9點的服務和廣播
我打電話setAlarm()
方法,當我的應用程序啓動第一time.App在那個時候自動打開Notification
產生(First Launch time)
不斷射擊同樣的通知再次n和interval time
約2 min
。
注意:生成通知不是問題。
public void setAlarm() {
alarmManager = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE);
alarmIntent = new Intent(getActivity(), AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, alarmIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 17);
calendar.set(Calendar.MINUTE, 15);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
//alarmManager.setRepeating(AlarmManager.RTC, AlarmManager.INTERVAL_HALF_DAY, AlarmManager.INTERVAL_HALF_DAY, pendingIntent);
}
AlarmService
public class AlarmReceiver extends BroadcastReceiver {
NotificationManager notificationManager;
@Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
MyAlarmSevice
public class MyAlarmService extends Service {
private static final int NOTIFICATION_ID = 1;
private NotificationManager notificationManager;
private PendingIntent pendingIntent;
private ArrayList<RandomQuotes> randomQuotesArrayList;
private PrefManager prefManager;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Context context = this.getApplicationContext();
prefManager = new PrefManager(this);
randomQuotesArrayList = prefManager.getSharedPreferenceStringList(this, "quote");
notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, MainActivity.class);
pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setVibrate(new long[]{5, 5, 5, 5, 5});
builder.setLights(Color.RED, 3, 3);
builder.setContentTitle("Quote Of the Day");
builder.setContentText(randomQuotesArrayList.get(0).getRandomQuote() + "\n\n" + "-" + randomQuotesArrayList.get(0).getRandomQuoteAuthor());
String longText;
if (!randomQuotesArrayList.get(0).getRandomQuoteAuthor().isEmpty()) {
longText = randomQuotesArrayList.get(0).getRandomQuote() + "\n\n" + " -" + randomQuotesArrayList.get(0).getRandomQuoteAuthor();
} else {
longText = randomQuotesArrayList.get(0).getRandomQuote() + "\n" +randomQuotesArrayList.get(0).getRandomQuoteAuthor();
}
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(longText));
builder.setSmallIcon(R.drawable.qt);
builder.setContentIntent(pendingIntent);
builder.setDefaults(Notification.DEFAULT_SOUND)
.setDefaults(Notification.DEFAULT_ALL).setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
;
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
}
完美! WakeUpAlarmReceiver是新的:D –
@Nils PLZ解釋** if(cur.after(calendar)){ calendar.add(Calendar.DATE,1); } ** – Omi