0
在我的項目中,每次手機啓動時都會調用RTC_WAKEUP方法,而不管我發佈通知的時間或日期。如何阻止RTC_WAKEUP在每次手機啓動時被調用?
我的問題是,我怎麼才能讓手機發出通知,當用戶想要通知,而不是自動啓動?
我長變量timeDateNotification可以在這些內部類中找到:
public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
cal.set(Calendar.MINUTE, view.getCurrentMinute());
cal.set(Calendar.HOUR_OF_DAY, view.getCurrentHour());
timeDateNotification = cal.getTimeInMillis();
}
}
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
cal.set(Calendar.YEAR, view.getYear());
cal.set(Calendar.MONTH, view.getMonth());
cal.set(Calendar.DAY_OF_MONTH, view.getDayOfMonth());
timeDateNotification = cal.getTimeInMillis();
}
}
這是導致所有問題的方法。
Intent intent = new Intent(getActivity(), AlertClass.class);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, timeDateNotification, PendingIntent.getBroadcast(getActivity()
.getApplicationContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));
Toast.makeText(getActivity(), "Notification sent!" ,Toast.LENGTH_SHORT).show();
這裏是AlertClass:
public class AlertClass extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
CreateNotification(context, "Forgetting something?", "Click here for more details", "Alert");
}
private void CreateNotification(Context context, String title, String text, String alert) {
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivityTrekkly.class), 0);
NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
.setTicker(alert)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(text)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true);
notification.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification.build());
}
}
謝謝。