0

나는 아침부터 이것을 찾고 있었으며 stackoverflow의 대부분의 Android 알람 문제를 언급했습니다.Android 알람이 새로운 활동을 시작하고 알람을 재설정합니다.

다른 의도로 여러 개의 알람을 설정하려고합니다. 알람을 받으면 알람을 취소하고 활동이 이미 실행중인 경우 앞에 오거나, 살해 된 경우 다시 시작하기를 원합니다.하지만 이번에는 알람을 다시 설정하지 않아야합니다. 나는 다른 경보가 실행되기를 원하지 않는다. 현재 문제는 알림을 클릭하면 활동이 다시 시작되고 경보가 재설정된다는 것입니다. alarmmanager.cancel을 사용하여 취소하려고하면 사용자에게 전혀 알리지 않습니다. 여기, 당신은 추가로 알림에서 출시를 구별 할 수 있습니다, 당신의 MainActivity에서

내 MainActivity 이잖아 여러 경보

을 설정
public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Calendar cal = Calendar.getInstance();  //for using this you need to import java.util.Calendar; 

    // add minutes to the calendar object 
    cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY); 
    cal.set(Calendar.HOUR_OF_DAY, 22); 
    cal.set(Calendar.MINUTE, 8); 
// cal.add(Calendar.MINUTE, 1); 
    AlarmManager mgrAlarm = (AlarmManager) this.getSystemService(ALARM_SERVICE); 
    ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>(); 

    for(int i = 0; i < 10; ++i) 
    { 
     Intent intent = new Intent(this, AlarmReceiver.class); 
     intent.putExtra("title", "notification no."+String.valueOf(i)); 
     intent.putExtra("NOTIFICATION_ID", String.valueOf(i)); 
     // Loop counter `i` is used as a `requestCode` 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, i, intent, 0); 
     // Single alarms in 1, 2, ..., 10 minutes (in `i` minutes) 
     mgrAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
        SystemClock.elapsedRealtime() + 60000 * i, 
        pendingIntent); 

     intentArray.add(pendingIntent); 
    } 
} 

내 AlarmReceiver 클래스

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

    NotificationManager manger = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(R.drawable.ic_launcher, "Alarm App", System.currentTimeMillis()); 

      Bundle extras=intent.getExtras(); 
      String title=extras.getString("title"); 
      int notif_id=Integer.parseInt(extras.getString("NOTIFICATION_ID")); 

    //here we get the title and description of our Notification 
       Class myclass = MainActivity.class; 
      PendingIntent contentIntent = PendingIntent.getActivity(context, notif_id, 
        new Intent(context, MainActivity.class), 0); 
      String note=extras.getString("note"); 
      notification.setLatestEventInfo(context, note, title, contentIntent); 
      notification.flags = Notification.FLAG_INSISTENT; 
      notification.defaults |= Notification.DEFAULT_SOUND; 
    //here we set the default sound for our 
    //notification 
      // The PendingIntent to launch our activity if the user selects this notification 
      manger.notify(notif_id, notification); 
} 

}; 

답변

0

도와주세요 내 코드입니다 매개 변수. 특정 알림을 취소하려면 알림 ID가 필요합니다. 그래서, 당신은 파일 AlarmReceiver.java에서 당신의 MainActivity

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

     Intent appIntent = this.getIntent(); 
     int notif_id = appIntent.getIntExtra("my_notification_id", -1) ; 
     if(notif_id != -1) 
     { 
      Log.d ("LOG_TAG", "Launched from Notification "); 
      NotificationManager nm = (NotificationManager) getSystemService( NOTIFICATION_SERVICE); 
      nm.cancel(notif_id); 

      /* Do the separate processing here */ 
      ..... 
     } 
     else 
     { 
      /* Your previous onCreate code goes here */ 

에서 다음을 시도 할 수 있습니다, 당신은 다음이 도움이

//PendingIntent contentIntent = PendingIntent.getActivity(context, notif_id, new   Intent(context, MainActivity.class), 0); 

Intent appIntent = new Intent( context, MainActivity.class); 
appIntent.putExtra("my_notification_id", notif_id); 
PendingIntent contentIntent = PendingIntent.getActivity(context, notif_id, appIntent, 0); 

희망을 변경 확인해야합니다.

+0

고맙습니다 ... 도움이되었습니다. – user2184523

관련 문제