通知显示何时不应该

时间:2016-04-08 18:29:22

标签: java android android-studio

我正在处理必须在用户选择的特定时间显示的通知。

但是当我运行它时,通知会显示但不会在选定的时间显示,只有当我在时间选择器中询问时间时才显示,甚至在选择时间之前。

是否有人知道如何更改代码,以便通知仅在所选时间出现?

showDialog(DIALOG_ID);  //this is the dialog that asks for an hour and minute.


alarmMethod1();


private void alarmMethod1(){


       Calendar calendari1 = Calendar.getInstance();
       calendari1.set(Calendar.HOUR_OF_DAY, hour_x);
       calendari1.set(Calendar.MINUTE, minute_x);
       calendari1.set(Calendar.SECOND, 00);

       Intent myIntent1 = new Intent(Main2Activity.this, NotifyService1.class);
       AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
       pendingIntent1 = PendingIntent.getService(Main2Activity.this, 0, myIntent1, 0);
       alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP, calendari1.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent1);

}

然后这是意图所在的类:

public class NotifyService1 extends Service {

@Override
public IBinder onBind(Intent intent) {
    return null;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onCreate(){
    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


    NotificationManager nNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(),Main3Activity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);

    Notification mNotify = new Notification.Builder(this)
            .setContentTitle("Hora d'entrenar!")
            .setContentText("Clica per començar entrenament de pit i braços")
            .setSmallIcon(R.drawable.logofinal)
            .setContentIntent(pIntent)
            .setSound(sound)
            .build();

    nNM.notify(1,mNotify);

}

}

1 个答案:

答案 0 :(得分:0)

自从Android API 19以来,为了减少电池消耗,所有警报都不准确,这意味着您的警报不会在您需要时准确触发,但如果您确实需要这样做,则需要使用setExact然后再写你自己重复报警处理程序,不再是“设置精确和重复”。请看:

http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int, long, long, android.app.PendingIntent)

http://developer.android.com/reference/android/app/AlarmManager.html#setExact(int, long, android.app.PendingIntent)