我正在制作一个应用程序,我希望用户在共享的偏好中指定时间,然后我将使用该时间启动通知,无论它们是否在应用中,像user-defined alarm notification
。
我已经尝试了所有事情,但我不知道如何使它起作用。如果偏好(复选框)为TRUE,则应在该特定时间每天出现通知。
在首选项中设置剩余
public static void setReminder(Context context,Class<?> cls,int hour, int min) {
Calendar calendar = Calendar.getInstance();
Calendar setcalendar = Calendar.getInstance();
setcalendar.set(Calendar.HOUR_OF_DAY, hour);
setcalendar.set(Calendar.MINUTE, min);
setcalendar.set(Calendar.SECOND, 0);
// cancel already scheduled reminders
cancelReminder(context,cls);
if(setcalendar.before(calendar))
setcalendar.add(Calendar.DATE,1);
// Enable a receiver
ComponentName receiver = new ComponentName(context, cls);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Intent intent1 = new Intent(context, cls);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
DAILY_REMINDER_REQUEST_CODE, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP,setcalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
触发ala-ram
public class AlarmReceiver extends BroadcastReceiver {
String TAG = "AlarmReceiver";
@Override
public void onReceive(Context context, Intent intent) {
//Trigger the notification
NotificationScheduler.showNotification(context, MainActivity.class,
"You have 5 unwatched videos", "Watch them now?");
}
}
显示通知
public static void showNotification(Context context,Class<?> cls,String title,String content)
{
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent notificationIntent = new Intent(context, cls);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(cls);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(
DAILY_REMINDER_REQUEST_CODE,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle(title)
.setContentText(content).setAutoCancel(true)
.setSound(alarmSound).setSmallIcon(R.mipmap.ic_launcher_round)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(DAILY_REMINDER_REQUEST_CODE, notification);
}
显示时间选择器对话框。
private void showTimePickerDialog(int h, int m)
{
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.timepicker_header, null);
TimePickerDialog builder = new TimePickerDialog(this, R.style.DialogTheme,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hour, int min) {
Log.d(TAG, "onTimeSet: hour " + hour);
Log.d(TAG, "onTimeSet: min " + min);
localData.set_hour(hour);
localData.set_min(min);
tvTime.setText(getFormatedTime(hour,min));
NotificationScheduler.setReminder(MainActivity.this,AlarmReceiver.class,
localData.get_hour(),localData.get_min());
}
}, h, m, false);
builder.setCustomTitle(view);
builder.show();
}
最后,我发现了如何做。对于可能需要相同的任何人,我将发布代码:
这是一次在活动中调用通知的方法:
public void notifyAtTime() {
sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
long time = sharedPref.getLong("Timepref", 0);
SimpleDateFormat formatter = new SimpleDateFormat("HH", Locale.UK);
SimpleDateFormat formatter1 = new SimpleDateFormat("mm", Locale.UK);
Date date = new Date(time);
Date date1 = new Date(time);
long hour = Long.parseLong(formatter.format(date));
long minute = Long.parseLong(formatter1.format(date1));
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, (int) hour);
cal.set(Calendar.MINUTE, (int) minute);
cal.set(Calendar.SECOND,0);
if (alarmManager != null) {
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, broadcast);
}
}
这是接收者
public class AlarmReceiver extends BroadcastReceiver {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, TicketActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(TicketActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String ringer = sharedPreferences.getString("key_notifications_new_message_ringtone","");
boolean vibrate = sharedPreferences.getBoolean("Vibrate",true);
Uri ringtone = Uri.parse(ringer);
Notification notification = builder.setContentTitle(context.getResources().getString(R.string.notific))
.setContentText(context.getResources().getString(R.string.notific))
.setTicker(context.getResources().getString(R.string.notific))
.setSmallIcon(R.mipmap.ic_launcher)
.setSound(ringtone)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(0, notification);
}
}}