2013-07-19 90 views
-1

我想每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); 

    } 
+0

問題是什麼? –

+0

對不起,如果我的英語不正確我不是母語的人,我的問題是我如何才能在重複鬧鐘的預定時間(僅每三個小時)顯示通知,並停止在有人重新啓動手機時發送通知 –

+0

是從你的服務器發送的通知還是什麼? –

回答

0

在您的代碼中,您要求Alarm第一次立即關閉Alarm已設置。

試試這個 am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() + 3*60*60*1000, 3*60*60*1000 , pi);

+0

對不起我的問題,但我在編程方面是新的,calendar.getTimeInMillis()reutrn當前時間?不是我設定日曆的時間?通過以下代碼:calendar.set(Calendar.HOUR_OF_DAY,sp); calendar.set(Calendar.MINUTE,00); calendar.set(Calendar.SECOND,00); –

+0

如果你閱讀'AlarmManager'文檔,你會知道第二個'parameter'告訴'AlarmManager'什麼時候觸發第一個'Alarm',如果這個時間已經過去了,那麼它會立即觸發報警。在這種情況下,使用正確的'firstfire'時間將解決您的問題。什麼'日曆'給你回來應該是另一個qustion,你可以發佈一個單獨的問題或尋找發佈的問題.. – Varun

+0

是的,你是對的,你提議我是正確的,但我想立即觸發警報,如果手機打開在(10,13等)這就是爲什麼我試圖通過何時從我的共享首選項觸發報警。我發現我沒有收到什麼問題,並正確設置了我的共享首選項。我替換了代碼,現在它正在工作!非常感謝您的時間和幫助 –

相關問題