在我的项目中,无论我发出通知的时间或日期,每次手机启动时都会调用RTC_WAKEUP方法。
我的问题是,我怎样才能让手机在用户想要通知时发出通知而不是在启动时自动发出通知?
我的long变量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());
}
}
谢谢。
答案 0 :(得分:0)
以下代码解决了此问题。首先关闭接收器然后取消通知。
ComponentName receiver = new ComponentName(getActivity(), AlertClass.class);
PackageManager pm = getActivity().getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Intent intent = new Intent(getActivity(), AlertClass.class);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(PendingIntent.getBroadcast(getActivity()
.getApplicationContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));