我想每10小時到22點每三小時顯示一次通知,但是我的應用程序在用戶打開手機時也會顯示通知。爲了在每次通知過程被觸發時解決這個問題,我在我的共享偏好設置中將當前時間加上3個小時。我使用這個時間值來觸發我重複的鬧鐘,但它似乎不能正常工作。Android:每當手機重新啓動時都會發送通知
如何僅在重複鬧鐘的預定時間(每三小時)顯示通知,並在有人重新啓動手機時停止發送通知?
我的代碼如下:
廣播接收機,它trggered wheneven用戶重新啓動他們的電話是調用下面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);
}
問題是什麼? –
對不起,如果我的英語不正確我不是母語的人,我的問題是我如何才能在重複鬧鐘的預定時間(僅每三個小時)顯示通知,並停止在有人重新啓動手機時發送通知 –
是從你的服務器發送的通知還是什麼? –