2016-07-29 70 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