2016-07-29 13 views
0

BroadcastReceiverから通知を送信したいと思います。 AlarmReceiverによって送信された通知が、通知をクリックしたときに再び通知を送信しました。同じことは、ここで通知がクリックされたときに継続通知を避ける方法

はそれがMainActivityを開き、このonCreate方法で通知をクリック上のように私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

で何度も何度も無限ループのように、再び起こりますあなたはsetAlarm()と呼んでいます。したがって、通知onCreateのメソッドが呼び出されたときにsetAlarm()が呼び出されると、アラームとビルドの通知が再度設定されます。

は、次の操作を行い、それが自動的にonCreate活動の呼び出されないように、ボタンの

コールsetAlarm()onClickを変更します。

あなたは通知に今、あなたは通知のクリックでManinActivityを開くために意図を使用しているのようnotification

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

の自動

変更の意図を送信する場合。

変更Intent OTHERACTIVITYがonCreate方法でnotificationを構築しませsetAlarm()かない活動である

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

へ。

代替方法

使用sharedPreferences通知は一度か構築するかどうかを確認します。ビルドが一旦setAlarm()

+0

通知は自動的に送信されるはずですので、私は 'onClick'を使用できません – emrekose26

+0

@ emrekose26私は自分の答えを編集しました。問題があればお知らせください –

+0

ありがとうございますが、通知をクリックしたときには他のアクティビティは開かないようにしてください。 – emrekose26

関連する問題