2016-07-29 2 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.javaMainActivity을 열 통지에 클릭에로이의 onCreate 방법

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

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

에 OtherActivity는 onCreate 방법 notification를 구축하지 setAlarm() 또는 수행하는 활동이다.

다른 방법

사용 sharedPreferences 알림 한번 여부를 구축 여부를 확인합니다. 빌드에 한 번 setAlarm()

+0

알림이 자동으로 전송되어야하므로 'onClick'을 사용할 수 없습니다. – emrekose26

+0

@ emrekose26 답변을 편집했습니다. 어떤 문제인지 알려주세요 –

+0

답변 해 주셔서 감사합니다.하지만 알림을 클릭 할 때 다른 활동이 열리지 않아야합니다. – emrekose26

관련 문제