我正在创建一个应用,我希望用户在共享偏好设置中指定时间,然后我将利用该时间发布通知,无论他们是否在应用中,如user-defined alarm notification
。
我已经尝试了一切,但我不知道如何让它发挥作用。如果首选项(复选框)为真,则通知应该在该特定时间每天出现。
答案 0 :(得分:4)
在首选项中设置余数
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();
}
答案 1 :(得分:0)
最后我发现了如何做到这一点。对于可能需要相同的人,我会发布代码:
这是在一旦时间正确的情况下调用活动中的通知的方法:
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);
}
}}