要求: -
我需要在一周的特定日期和警报开始的日期发出警报(例如我想在2017年5月26日下午6:45开始的每周的周五和周六发出警报)
问题: -
当从循环内调用AlarmManager onReceive()
,setInexactRepeating
或setWindow
方法时,BroadcastReceiver的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>
答案 0 :(得分: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>