要求: -
我需要火在每周的指定日期并从报警将开始日期(比如我想在6:45 PM火上周五,每周周六警报从2017年5月26日开始)报警
问题:-
广播接收器的onReceive()
方法不执行时AlarmManager setInexactRepeating
, setWindow
或setRepeating
方法是从一个循环中调用。
用于此任务的代码是下面提到: -
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>