使用setExactAndAllowWhileIdle每天发送通知时,仅一天有效

时间:2019-04-06 09:40:49

标签: android alarmmanager

当我使用AlarmManager.SetRepeating()每天发送两次通知时,对于api <23来说效果很好 在api23及更高版本中,它不会发送任何通知,因此我将setExactAndAllowWhileIdle()用于Build.VERSION.SDK_INT> = 23 它仅在第一天有效,之后就停止了

public void OnRepeatingNotification() {
    String ACTION_ONE = "android.intent.action.ACTION_ONE";
    String ACTION_TWO = "android.intent.action.ACTION_TWO";
    String title = getResources().getString(R.string.app_name);
    String content1 = getResources().getString(R.string.alarmone);
    String content2 = getResources().getString(R.string.alarmtwo);
    boolean sound=settings.getBoolean("sett_1",false);
    boolean vibrate=settings.getBoolean("sett_2",false);
    String[] parts = getTimes();
    String sunriseTime = parts[0].substring(0, 5) + " " + parts[0].substring(5);
    String[] sunriseTimeArray = sunriseTime.split(" ");
    String[] sunriseDivision = sunriseTimeArray[0].split(":");
    String sunsetTime = parts[1].substring(0, 5) + " " + parts[1].substring(5);
    String[] sunsetTimeArray = sunsetTime.split(" ");
    String[] sunsetDivision = sunsetTimeArray[0].split(":");
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(System.currentTimeMillis());
    Intent myIntent = new Intent(this, NotificationReciever.class);
    myIntent.setAction(ACTION_ONE);
    myIntent.putExtra("title", title);
    myIntent.putExtra("content1", content1);
    myIntent.putExtra("sound",sound);
    myIntent.putExtra("vibrate",vibrate);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1253, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    cal.set(Calendar.HOUR_OF_DAY, h1);
    cal.set(Calendar.MINUTE, Integer.parseInt(sunriseDivision[1]));
    cal.set(Calendar.SECOND, 0);
    //alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
    //AlarmManager.INTERVAL_DAY, pendingIntent);
    AlarmManagerCompat.setExactAndAllowWhileIdle(alarmManager, AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),
    pendingIntent);
    Intent myIntent2 = new Intent(this, NotificationReciever.class);
    myIntent2.setAction(ACTION_TWO);
    myIntent2.putExtra("title", title);
    myIntent2.putExtra("content2", content2);
    myIntent2.putExtra("sound",sound);
    myIntent2.putExtra("vibrate",vibrate);
    PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 1263, myIntent2,
            PendingIntent.FLAG_CANCEL_CURRENT);
    // Set the time for second alarm here
    cal.setTimeInMillis(System.currentTimeMillis());
    cal.set(Calendar.HOUR_OF_DAY, h2);
    cal.set(Calendar.MINUTE, Integer.parseInt(sunsetDivision[1]));
    cal.set(Calendar.SECOND, 0);
    //alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
    //AlarmManager.INTERVAL_DAY, pendingIntent2);

    AlarmManagerCompat.setExactAndAllowWhileIdle( alarmManager, AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            pendingIntent2 );
}

用于发送通知的广播接收器的代码

@Override
public void onReceive(final Context context, Intent intent) {

        String title = intent.getStringExtra("title");
        String content1 = intent.getStringExtra("content1");
        String content2 = intent.getStringExtra("content2");
        boolean vibrate = intent.getBooleanExtra("vibrate", false);
        boolean sound = intent.getBooleanExtra("sound", false);
        NotificationID n=new NotificationID();
       int NOTIFICATION_ID = n.getID();
        Intent in = new Intent(context, DailyAzkaar.class);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    //If on Oreo then notification required a notification channel.
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("default", "Default", NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"default");
        builder.setSmallIcon(R.mipmap.ic_launcher);
        if (vibrate == true)
            builder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
        if (sound == true) {
            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            builder.setSound(alarmSound);
        }
        builder.setContentTitle(context.getString(R.string.app_name));
        if (intent.getAction().equalsIgnoreCase("android.intent.action.ACTION_ONE")) {
            in.putExtra("activity", 1);
            in.putExtra("poss", 0);// new
            in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 1253, in, PendingIntent.FLAG_ONE_SHOT);
            builder.setContentText(content1);
            builder.setContentTitle(title);
            builder.setContentIntent(pendingIntent).setAutoCancel(true);
        } else {
            in.putExtra("activity", 2);
            in.putExtra("poss", 0);// new
            in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 1263, in, PendingIntent.FLAG_ONE_SHOT);
            builder.setContentText(content2);
            builder.setContentTitle(title);
            builder.setContentIntent(pendingIntent).setAutoCancel(true);

        }
        Notification notification = builder.build();
        //int notificationID = 0;
        notificationManager.notify(NOTIFICATION_ID, notification);

}

1 个答案:

答案 0 :(得分:0)

首先,可以使用AlarmManagerCompat来代替检查api版本以使用AlarmManager的方法:

AlarmManagerCompat.setExactAndAllowWhileIdle(
     @NonNull AlarmManager alarmManager, 
     int type,
     long triggerAtMillis, 
     @NonNull PendingIntent operation
)

然后尝试设置不同的操作并为您的意图请求代码。您可以使用毫秒为单位的时间进行操作并请求代码,如下所示:

myIntent.setAction(cal.getTimeInMillis());

,以及待定的意向更改请求代码和标记为FLAG_CANCEL_CURRENT。因此,您可以这样做:

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, cal.getTimeInMillis(), myIntent, PendingIntent.FLAG_CANCEL_CURRENT);