AlarmManager setInexactRepeating,setWindow,setRepe

2019-05-12 13:12发布

要求: -
我需要火在每周的指定日期并从报警将开始日期(比如我想在6:45 PM火上周五,每周周六警报从2017年5月26日开始)报警

问题:-
广播接收器的onReceive()方法不执行时AlarmManager setInexactRepeatingsetWindowsetRepeating方法是从一个循环中调用。

用于此任务的代码是下面提到: -
AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hiappz.remindersample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/MainThemeBlueAccent">
        <activity android:name=".Activities.MainActivity">
           <intent-filter>
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
        </activity>

        <activity
            android:name=".Activities.ActivitySaveReminder"
            android:screenOrientation="portrait"
            android:theme="@style/MainThemeBlueAccent"
            android:windowSoftInputMode="adjustNothing" />

        <receiver android:name=".Activities.MedicineReminderAlarmReceiver"
            android:process=":remote">
            <!--<intent-filter>
                <action android:name="android.intent.action.NOTIFY"/>
            </intent-filter>-->
        </receiver>

        <receiver android:name=".Activities.AlarmBootCompletedReceiver"
            android:exported="false"
            android:enabled="true"
           android:permission="android.permission.RECEIVE_BOOT_COMPLETED">  
           <intent-filter>
             <action android:name="android.intent.action.BOOT_COMPLETED"/>
             <category android:name="android.intent.category.DEFAULT"/>
           </intent-filter>
        </receiver>

    </application>
  <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
</manifest>

ActivitySaveReminder.java

public class ActivitySaveReminder extends AppCompatActivity{

    onCreate() {
        fireAlarmOnWeekDays();
    }

    public void fireAlarmOnWeekDays() {
        long[] weekDaysMillisecondsArr = {1495737000000l, 1495823400000l};
        long[] alarmStartMillisArr = {1495801680000l};
        Calendar calendar = Calendar.getInstance();

        for (int i = 0; i < weekDaysMillisecondsArr.length; i++) {
            calendar.setTimeInMillis(weekDaysMillisecondsArr[i]);
            Log.d(TAG, "fireAlarmOnWeekDays: -->> selected day time stamp --> "+calendar.getTime());

            for (int j = 0; j < alarmStartMillisArr.length; j++) {
                int id = new Random().nextInt();
                Intent intentRemiderAlarm = new Intent(mContext, MedicineReminderAlarmReceiver.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, id, intentRemiderAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
                pendingIntentsList.add(pendingIntent);

                if (Build.VERSION.SDK_INT >= 19) {
                    alarmManager.setWindow(AlarmManager.RTC_WAKEUP, alarmStartMillisArr[j], AlarmManager.INTERVAL_DAY * 7, pendingIntent);
//                    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmStartMillisArr[j], AlarmManager.INTERVAL_DAY * 7, pendingIntent);
//                    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, alarmStartMillisArr[j], AlarmManager.INTERVAL_DAY * 7, pendingIntent);
                } else {
                    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, alarmStartMillisArr[j], AlarmManager.INTERVAL_DAY * 7, pendingIntent);
                }
                Log.d(TAG, "fireAlarmOnWeekDays: -->> alarm start time -->> "+alarmStartMillisArr[j] + " id -->> "+id);
            }
        }
    }
}

MedicineReminderAlarmReceiver.java

ublic class MedicineReminderAlarmReceiver extends BroadcastReceiver {
    private final String TAG = "MedicineRemindrAlrmRcvr";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "onReceive: -->> executed");
            Log.d(TAG, "onReceive: -->> System.currentTimeMillis() -->> "+ System.currentTimeMillis());
        }
    }
}

styles.xml

<style name="MainThemeBlueAccent" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/status_bar_color</item>
        <item name="colorPrimaryDark">@color/status_bar_color</item>
        <item name="colorAccent">@color/blue_color</item>
        <item name="android:actionMenuTextColor">@color/text_heading_color_black</item>
        <item name="actionMenuTextColor">@color/text_heading_color_black</item>
        <item name="android:textColorSecondary">@color/text_sub_heading_color_light_black</item>
    </style>

strings.xml中

<string name="app_name">ReminderSample</string>

Answer 1:

试试这个代码的工作周五报警,同样可以为周六首先设置你必须注册您的报警接收器和报警时间

public static void SetAlarmForFriday(Context mContext) {
        try {

            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
            System.out.println("Date " + calendar.getTime());

            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH) + 1;
            int day = calendar.get(Calendar.DAY_OF_MONTH);

            String DateToConvert = day + "," + month + "," + year + " 10:10 AM";
            Date date = new SimpleDateFormat("dd,MM,yyyy hh:mm a", Locale.ENGLISH).parse(DateToConvert);

            Intent intent = new Intent(mContext, AlarmReceiver.class);
            intent.setAction("setYourActionHere");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(), 234324243, intent, 0);
            AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(mContext.ALARM_SERVICE);

            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, date.getTime(), AlarmManager.INTERVAL_DAY, pendingIntent);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

使接收机类

public class AlarmReceiver extends BroadcastReceiver {
    public AlarmReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.


    }

}

注册您的广播接收器下应用标签清单文件

<receiver
            android:name=".AlarmReceiver"
            android:enabled="true"
            android:exported="true"></receiver>


文章来源: AlarmManager setInexactRepeating, setWindow, setRepeating methods do not fire alarm when called from within loop for week days