2016-07-29 59 views
0

我想通過BroadcastReceiver發送通知。由AlarmReceiver發送的通知,但當我點擊通知時再次發送通知。同樣的事情再次發生,一次又一次地像無限循環如何在點擊通知時避免連續通知

這裏是我的AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 

    android.support.v7.app.NotificationCompat.Builder builder = new android.support.v7.app.NotificationCompat.Builder(context); 

    builder.setSmallIcon(R.mipmap.ic_launcher); 

    // This intent is fired when notification is clicked 
    Intent i = new Intent(context, MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0); 

    // Notifcation notify sound 
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    // Set the intent that will fire when the user taps the notification. 
    builder.setContentIntent(pendingIntent); 

    // Large icon appears on the left of the notification 
    builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)); 

    // Content title, which appears in large type at the top of the notification 
    builder.setContentTitle("Have a good weekend"); 

    //Notification click after clear notification 
    builder.setAutoCancel(true); 

    //Set notification sound 
    builder.setSound(alarmSound); 

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

    // Will display the notification in the notification bar 
    notificationManager.notify(1, builder.build()); 

    } 
} 

MainActivity.java

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    setAlarm(); 
} 

private void setAlarm(){ 
    AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
    Intent alarmIntent = new Intent(MainActivity.this, MyAlarmReceiver.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0); 

    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 

    calendar.set(Calendar.DAY_OF_WEEK,6); 
    calendar.set(Calendar.HOUR_OF_DAY,16); 
    calendar.set(Calendar.MINUTE,53); 
    calendar.set(Calendar.SECOND,0); 

    manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent); 

    } 
} 
+1

喜@ emrekose26,你必須面對這樣的問題,因爲你在設置一套報警() 創建活動,當你收到通知,點擊通知,然後MainActivity是開放的。所以,再次調用setAlarm()再次發出通知。 –

回答

0

上點擊通知它打開MainActivity並在此onCreate方法您致電setAlarm()的活動。所以當點擊通知onCreate方法被調用,然後調用setAlarm(),這又設置了報警並建立通知。

待辦事項以下更改

通話setAlarm()按鈕onClick,所以它不會自動調用活動onCreate

如果您要發送通知的notification

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

截至目前您正在使用意圖的通知點擊打開ManinActivity自動

變化的意圖。

變化Intent

Intent i = new Intent(context, OtherActivity.class); 

OtherActivity是確實在onCreate方法不setAlarm()或構建notification活性。

替代方法

使用sharedPreferences檢查通知是否是一次構建,還是不行。 如果一次構建,那就不叫setAlarm()

+0

通知應該自動發送,因此我不能使用'onClick' – emrekose26

+0

@ emrekose26我編輯了我的答案。讓我知道任何問題 –

+0

謝謝你的回答,但其他活動不應該在點擊通知時打開。 – emrekose26