1

나는 약 알리미를하려고합니다. 여러 알림을 만들 때 내 앱은 모든 알림을 표시하지만 마지막 시간에만 설정합니다. 또한 OnReceive() 메서드를 더 많이 실행하며 이유를 이해하지 못합니다. 나는 같은 논쟁으로 많은 질문을 발견하고 그 답변으로 내 코드를 변경했지만 아무런 문제가 해결되지 않았습니다. 고맙습니다!알람 관리자가있는 여러 번 반복 된 알림 [안드로이드]

장치 : Asus ZenPad S 8.0; 안드로이드 : 6.0.1;

이 내 코드입니다 :

@Override 
public void onClick(View view) { 
    switch (view.getId()) { 

     case R.id.button: 
      mcurrentTime = Calendar.getInstance(); 
      int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY); 
      int minute = mcurrentTime.get(Calendar.MINUTE); 
      TimePickerDialog mTimePicker; 
      mTimePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { 
       @Override 
       public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) { 
        mcurrentTime.set(Calendar.HOUR_OF_DAY, selectedHour); 
        mcurrentTime.set(Calendar.MINUTE, selectedMinute); 
        setAlarm(); 

       } 
      }, hour, minute, true);//Yes 24 hour time 
      mTimePicker.setTitle(R.string.orario); 
      mTimePicker.show(); 
      notificationAlarm = new NotificationAlarm(editText.getText().toString()); 
      getApplicationContext().registerReceiver(notificationAlarm, intentFilter); 
      break; 
}} 


private void setAlarm(){ 
    int id = (int) System.currentTimeMillis(); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this,id, intent, PendingIntent.FLAG_ONE_SHOT); 
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, mcurrentTime.getTimeInMillis(),1000*60*5, pendingIntent); 
    Log.d("Time", String.valueOf(mcurrentTime.getTimeInMillis())); 

} 



@Override 
public void onResume(){ 
    super.onResume(); 
    final String SOME_ACTION = "com.example.mypackage.a2_pillsrem_WAKE"; 
    intent = new Intent(SOME_ACTION); 
    intentFilter = new IntentFilter(SOME_ACTION); 
} 

그리고 이것은 브로드 캐스트 리시버 클래스입니다 :

public class NotificationAlarm extends BroadcastReceiver { 
String s; 
int mNotificationId; 
Random r = new Random(); 
ArrayList<Integer> notify = new ArrayList<>(); 
public NotificationAlarm(String s){ 
    this.s=s; 
} 

public void onReceive(Context context, Intent intent) { 
    //RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.notification); 

    Notification.Builder mBuilder = 
      new Notification.Builder(context) 
        .setSmallIcon(R.drawable.pill) 
        .setContentTitle(context.getString(R.string.titolo_notifica)) 
        .setContentText(context.getString(R.string.descr_notifica)+" "+s) 
        // .setContent(remoteViews) 
        .setAutoCancel(true) 
        .setDefaults(Notification.DEFAULT_SOUND) 
        .setVisibility(1);//1 è uguale a VISIBILITY_PUBLIC 
             // https://developer.android.com/reference/android/support/v4/app/NotificationCompat.html#VISIBILITY_PUBLIC 

    //Random r = new Random(); 
    mNotificationId = r.nextInt(10000); //assegno un id casuale ad ogni notifica affinchè non siano sovrascritte 
    Intent notificationIntent = new Intent(context,MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context,0,notificationIntent,0); 
    mBuilder.setContentIntent(pendingIntent); 
    Log.d("IDNOT",Integer.toString(mNotificationId)); 
    //Gets an instance of the NotificationManager service 
    NotificationManager mNotifyMgr = 
      (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); 
    // Builds the notification and issues it. 
    mNotifyMgr.notify(mNotificationId, mBuilder.build()); 
} 
} 
+0

이 시도. if (SDK_INT = Build.VERSION_CODES.M) { alarmManager.setExactAndAllowWhileIdle (AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, pendingIntent); } –

+0

SDK_INT의 의미는 무엇입니까 ?? –

+0

빌드 번호, 실행중인 버전 장치가 있는지 확인 –

답변

0

당신이 다음과 같은 PendingIntent.FLAG_UPDATE_CURRENT로 알람을 설정하는 시도? 그것을 밖으로 시도하십시오.

private void setAlarm() { 
     int id = (int) System.currentTimeMillis(); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, mcurrentTime.getTimeInMillis(), 1000 * 60 * 5, pendingIntent); 
     Log.d("Time", String.valueOf(mcurrentTime.getTimeInMillis())); 
    } 

희망이 있습니다.

+0

나는 이미 이와 같은 경고를 설정하려고 시도했지만 아무 것도 바뀌지 않았습니다! –

0

많은 시도 후에 BroadcastReceiver를 사용하지 않고 IntentServices를 사용하기로 결정했습니다. 이 오른쪽 코드와 솔루션입니다 :

@Override 
public void onClick(View view) { 
    switch (view.getId()) { 

     case R.id.button: 
      mcurrentTime = Calendar.getInstance(); 
      int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY); 
      int minute = mcurrentTime.get(Calendar.MINUTE); 
      TimePickerDialog mTimePicker; 
      mTimePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { 
       @Override 
       public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) { 
        mcurrentTime.set(Calendar.HOUR_OF_DAY, selectedHour); 
        mcurrentTime.set(Calendar.MINUTE, selectedMinute); 
        setAlarm(); 

       } 
      }, hour, minute, true);//Yes 24 hour time 
      mTimePicker.setTitle(R.string.orario); 
      mTimePicker.show(); 
      break; 

private void setAlarm(){ 
    int id = (int) System.currentTimeMillis(); 
    Intent intent = new Intent(this, NotificationAlarm.class); 
    intent.putExtra("Farmaco",editText.getText().toString()); 
    PendingIntent pendingIntent = PendingIntent.getService(this,id, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, mcurrentTime.getTimeInMillis(),5*60*1000, pendingIntent); 
    } 
    Log.d("Time", String.valueOf(mcurrentTime.getTimeInMillis())); 
} 

는 그리고이 알림 클래스입니다 :

public class NotificationAlarm extends IntentService { 
String s; 
int mNotificationId; 
Random r = new Random(); 
public NotificationAlarm(){ 
    super("HelloNotificationService"); 

} 

@Override 
protected void onHandleIntent(Intent intent) { 

    Notification.Builder mBuilder = 
      new Notification.Builder(getApplicationContext()) 
        .setSmallIcon(R.drawable.pill) 
        .setContentTitle(getApplicationContext().getString(R.string.titolo_notifica)) 
        .setContentText(getApplicationContext().getString(R.string.descr_notifica)+" "+s) 
        // .setContent(remoteViews) 
        .setAutoCancel(true) 
        .setDefaults(Notification.DEFAULT_SOUND) 
        .setVisibility(1);//1 è uguale a VISIBILITY_PUBLIC 
    // https://developer.android.com/reference/android/support/v4/app/NotificationCompat.html#VISIBILITY_PUBLIC 

    mNotificationId = r.nextInt(10000); //assegno un id casuale ad ogni notifica affinchè non siano sovrascritte 
    Intent notificationIntent = new Intent(getApplicationContext(),MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),(int)System.currentTimeMillis(),notificationIntent,PendingIntent.FLAG_ONE_SHOT); 
    mBuilder.setContentIntent(pendingIntent); 
    Log.d("IDNOT",Integer.toString(mNotificationId)); 

// Gets an instance of the NotificationManager service 
    NotificationManager mNotifyMgr = 
      (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE); 

// Builds the notification and issues it. 
    mNotifyMgr.notify(mNotificationId, mBuilder.build()); 
    stopSelf(); 
} 

public int onStartCommand(Intent intent, int flags, int startId){ 
    super.onStartCommand(intent, flags, startId); 
    s = intent.getStringExtra("Farmaco"); 
    return START_STICKY; 
} 
}