2017-02-13 92 views
-1

我的問題是當我設置2分鐘後的時間是10:02,當我重新啓動設備多次,可能是時間是10:03或10:04或10:05,我的通知每次總是顯示出來。AlarmManager設置失敗

我希望我的通知只在10:02後顯示一次,alarmManager是不是可以將它用於系統?

如果是這樣,我該如何避免這個問題?任何幫助將不勝感激。

我用了alarmManager,SharePreferenes,BroadcastReceiver。

我MainActivity按鈕功能:

public void onClickSetup(View view) 
    { 
     Toast.makeText(this,"Starting setting",Toast.LENGTH_SHORT).show(); 

     Calendar cal = Calendar.getInstance(); 
     // after 2 minutes 
     cal.add(Calendar.MINUTE, 2); 

     Intent intent = new Intent(this, PlayReceiver.class); 
     intent.putExtra("msg", "play_hskay"); 

     PendingIntent pi = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_ONE_SHOT); 

     AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 


     am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi); 

     long time=cal.getTimeInMillis(); 
     //Save the time for future 
     SharedPreferences preferences=getSharedPreferences("time",MODE_PRIVATE); 
     preferences.edit().putLong("saveTime",time) 
       .apply(); 
    } 

我給通知接收機:

public class PlayReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     Bundle bData = intent.getExtras(); 
     if (bData.get("msg").equals("play_hskay")) { 


      Intent notificationIntent = new Intent(context, MainActivity.class); 

      TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
      stackBuilder.addParentStack(MainActivity.class); 
      stackBuilder.addNextIntent(notificationIntent); 
      PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

      NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); 

      inboxStyle.addLine("message"); 

      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); 

      Notification notification; 
      notification = mBuilder.setSmallIcon(R.drawable.heart).setTicker("title").setWhen(0) 
        .setAutoCancel(true) 
        .setContentTitle("title") 
        //.setContentIntent(resultPendingIntent) 
        //.setSound(alarmSound) 
        .setStyle(inboxStyle) 
        //.setWhen(getTimeMilliSes(timeStamp)) 
        .setSmallIcon(R.mipmap.ic_launcher) 
        //.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon)) 
        .setContentText("title") 
        .setContentIntent(pendingIntent) 
        .build(); 

      NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

      notificationManager.notify(0 , notification); 
     } 
    } 

} 

我給重新啓動接收機的設備:

public class AlarmInitReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     Toast.makeText(context,"Start~~~~",Toast.LENGTH_LONG).show(); 
     // load system time for now. May be is 10:03、10:04、10:05 
     long current = Calendar.getInstance().getTimeInMillis(); 
     // load the time when i set it before. 
     SharedPreferences preferences = context.getSharedPreferences("time", context.MODE_PRIVATE); 
     long setTime = preferences.getLong("saveTime", 0); 


      Toast.makeText(context,"Enter receiver setting",Toast.LENGTH_LONG).show(); 

      Intent intentForSetTime = new Intent(context, PlayReceiver.class); 
      intentForSetTime.putExtra("msg", "play_hskay"); 

      PendingIntent pi = PendingIntent.getBroadcast(context, 1, intentForSetTime, PendingIntent.FLAG_ONE_SHOT); 

      AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
      am.set(AlarmManager.RTC_WAKEUP, setTime, pi);//setTime suppose is 10:02 

    } 
} 

我的清單爲廣播:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

<receiver android:name=".PlayReceiver"> 
      <intent-filter> 
       <action android:name="play_hskay" /> 
      </intent-filter> 
     </receiver> 

    <receiver android:name=".AlarmInitReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 
+0

您需要重置設備重啓完成時的所有報警。 – user1140237

+0

如何重置所有警報?你能給我看一些例子嗎? –

回答

1

在這種情況下,在不改變大部分代碼的情況下,只需在發生警報事件(即PlayReceiver)時向sharedprefs添加標誌。

如果警報發生在重新啓動之前,那麼boot_completed接收器將檢查prefs中的標誌是否已經完成。

然後可以設置/不設置報警。

乾杯...... !!

public void onClickSetup(View view) 
    { 
     Toast.makeText(this, "Starting setting", Toast.LENGTH_SHORT).show(); 

     Calendar cal = Calendar.getInstance(); 
     // after 2 minutes 
     cal.add(Calendar.MINUTE, 2); 

     Intent intent = new Intent(this, PlayReceiver.class); 
     intent.putExtra("msg", "play_hskay"); 

     PendingIntent pi = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_ONE_SHOT); 

     AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 


     am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi); 

     long time=cal.getTimeInMillis(); 
     //Save the time for future 
     //Code Edit 
     SharedPreferences preferences=getPreferences(MODE_PRIVATE); 
     preferences.edit().putBoolean("received",false).commit(); 
    } 





public class PlayReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     Bundle bData = intent.getExtras(); 
     if (bData.get("msg").equals("play_hskay")) { 


      Intent notificationIntent = new Intent(context, MainActivity.class); 

      TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
      stackBuilder.addParentStack(MainActivity.class); 
      stackBuilder.addNextIntent(notificationIntent); 
      PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

      NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); 

      inboxStyle.addLine("message"); 

      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); 

      Notification notification; 
      notification = mBuilder.setSmallIcon(R.drawable.heart).setTicker("title").setWhen(0) 
        .setAutoCancel(true) 
        .setContentTitle("title") 
          //.setContentIntent(resultPendingIntent) 
          //.setSound(alarmSound) 
        .setStyle(inboxStyle) 
          //.setWhen(getTimeMilliSes(timeStamp)) 
        .setSmallIcon(R.mipmap.ic_launcher) 
          //.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon)) 
        .setContentText("title") 
        .setContentIntent(pendingIntent) 
        .build(); 

      NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

      notificationManager.notify(0 , notification); 

      //Code Edit 

      SharedPreferences preferences=getPreferences(MODE_PRIVATE); 
      preferences.edit().putBoolean("received",true).commit(); 
     } 
    } 

} 




public class AlarmInitReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     //Code Edit 
     SharedPreferences preferences=getPreferences(MODE_PRIVATE); 
     boolean isReceived=preferences.getBoolean("received",false); 

     if(!isReceived) 
     { 
      Toast.makeText(context,"Start~~~~",Toast.LENGTH_LONG).show(); 
      // load system time for now. May be is 10:03、10:04、10:05 
      long current = Calendar.getInstance().getTimeInMillis(); 
      // load the time when i set it before. 
      SharedPreferences preferences = context.getSharedPreferences("time", context.MODE_PRIVATE); 
      long setTime = preferences.getLong("saveTime", 0); 


      Toast.makeText(context,"Enter receiver setting",Toast.LENGTH_LONG).show(); 

      Intent intentForSetTime = new Intent(context, PlayReceiver.class); 
      intentForSetTime.putExtra("msg", "play_hskay"); 

      PendingIntent pi = PendingIntent.getBroadcast(context, 1, intentForSetTime, PendingIntent.FLAG_ONE_SHOT); 

      AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
      am.set(AlarmManager.RTC_WAKEUP, setTime, pi);//setTime suppose is 10:02 

     } 
    } 
} 
+0

感謝回覆,請問如何爲SharePreferences設置一個標誌? –

+1

使用您使用的方法將共享首選項中的保存鍵值存儲在onclicksetup方法中。我只是想說,在播放接收器的共享前綴中添加任何布爾值或整型值,並帶有一些正值,這表示報警已經發生。 因此,在爲PlayReceiver設置鬧鐘時將檢查相同的值。 這裏是你的例子, 你可以在你的PlayReceiver中使用這個類, SharedPreferences prefs = this.getPreferences(MODE_PRIVATE); SharedPreferences.Editor prefsEditor = prefs.edit(); prefsEditor.putBoolean(「received」,true).commit(); –

+0

感謝@App開發人員,我在PlayReceiver類中嘗試您的代碼,並在AlarmInitReceiver中嘗試getBoolean。 問題依然存在:( 如果我getBoolean如果(真),但它仍然接收和發送通知 如果我getBoolean如果(假),它將會失去第一個通知時,我把它。 –

-1

我的代碼,我只需要啓動服務..我已經使用..

public void scheduleAlarm() { 
     // Construct an intent that will execute the AlarmReceiver 
     Intent intent = new Intent(this, LocationListnerServiec.class); 
     // Create a PendingIntent to be triggered when the alarm goes off 
     final PendingIntent pIntent = PendingIntent.getBroadcast(this, MyAlarmReceiver.REQUEST_CODE, 
       intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     // Setup periodic alarm every 5 seconds 
     long firstMillis = System.currentTimeMillis(); // alarm is set right away 
     AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
     // First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP 
     // Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY 
     alarm.setRepeating(AlarmManager.RTC_WAKEUP, firstMillis, 
       60000, pIntent); 
    } 

和MyAlaramReciver類

public class MyAlarmReceiver extends BroadcastReceiver { 
    public static final int REQUEST_CODE = 12345; 
    // Triggered by the Alarm periodically (starts the service to run task) 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent i = new Intent(context, LocationListnerServiec.class); 
     i.putExtra("foo", "bar"); 
     context.startService(i); 
     //Toast.makeText(context, "Welcome --------1", Toast.LENGTH_LONG).show(); 
    } 
} 

只是嘗試可能會幫助你。

+0

感謝您的回覆,但您的鬧鐘是重複的,我認爲它不像我的鬧鐘。 –