我希望从上午10点到晚上22点每隔三小时显示一次通知,但我的应用程序也会在用户打开手机时显示通知。为了在每次触发通知过程时解决此问题,我在共享首选项中设置当前时间加3小时。我使用这个时间值来触发我的重复警报,但它似乎无法正常工作。
如何仅在重复闹钟的预定时间(每三个小时)显示通知,并在有人重启手机时停止发送通知?
我的代码如下:
一个广播接收器,当用户重新启动手机时,它正在调用以下AlarmService:
public class AlarmService extends Service {
private static final String TAG = "MyService";
@Override
public void onStart(Intent intent, int startid) {
Calendar calendar ;
int sp= SharedPrefs.getDefaults("TIME", this);
Intent i = new Intent(this, NotificationBarAlarm.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, sp);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),3*60*60*1000 , pi);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service stopped", Toast.LENGTH_LONG).show();
Log.d("TAG", "onDestroy");
}
}
将Intent发送到以下广播接收器:
public class NotificationBarAlarm extends BroadcastReceiver {
NotificationManager notifyManager;
@Override
public void onReceive(Context context, Intent intent) {
Log.d("NotificationAlarm", "onReceive");
Time time = new Time();
long currenttimeMilliseconds = System.currentTimeMillis();
time.set(currenttimeMilliseconds);
int t = time.hour;
long updatedTime= currenttimeMilliseconds + 10800000;
Time nextalarmmill = new Time();
nextalarmmill.set(updatedTime);
int nextalarm = nextalarmmill.hour;
SharedPrefs.setDefaults("TIME", nextalarm, context);
Log.d("UPDATE TIME", "The next alarm is set at"+" "+ nextalarm);
notifyManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
if (t >= 10 && t <= 22) {
// This Activity will be started when the user clicks the notification
// in the notification bar
Intent notificationIntent = new Intent(context,
AlarmReceiverActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
Notification notif = new Notification(R.drawable.ic_launcher,
"A new notification just popped in!",
System.currentTimeMillis());
// sound when the notification shows up.
Uri alarmSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notif.sound = alarmSound;
notif.setLatestEventInfo(context, "Questionnaire",
"Please fill it up", contentIntent);
notifyManager.notify(1, notif);
}
}
}
Here is the code of the Shared Preferences:
public class SharedPrefs {
public static void setDefaults(String key, int value, Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(key, value);
editor.commit();
}
public static int getDefaults(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getInt(key, 10);
}
答案 0 :(得分:0)
在您的代码中,当设置Alarm
时,您首先要求Alarm
立即关闭。
试试这个
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() + 3*60*60*1000, 3*60*60*1000 , pi);