我想为某个日期和时间设置一次性闹钟。我也通过使用报警管理器来设置它。但我的问题是当设备关闭并打开然后警报没有唤醒。
我的代码::
public void setAlarm(String initialTime, String diffTime) throws ParseException {
Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
cal.set(Calendar.DATE,date);
cal.set(Calendar.MONTH,month-1);
cal.set(Calendar.YEAR,year);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, second);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(ChatScreen.this, AlarmNotificationReceiver.class);
i.putExtra("CoId", coId);
i.putExtra("DeptId", deptNo);
i.putExtra("CoName", companyName);
i.putExtra("DpLogo", dpLogo);
i.putExtra("DeptName",deptName);
i.setAction("isFromAlram");
PendingIntent pi = PendingIntent.getBroadcast(ChatScreen.this,0, i, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),pi);
}
My AlarmReceiverclass :::
public class AlarmNotificationReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.e("AlarmReceiver",action);
if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
Toast.makeText(context, "ALARM_BOOT_COMPLETED", Toast.LENGTH_LONG).show();
} else if (action.equals("isFromAlram")) {
Logger.errorLog("From Service", "yes");
}
}
}
并在menifest文件中给予权限android:name =" android.permission.RECEIVE_BOOT_COMPLETED"
接收器
android:name="com.chatapi.ChatApi.MyBroadcastReceiver"
android:enabled="true"
编辑此处为接收器的完整清单条目
<receiver android:name=".AlarmNotificationReceiver"
android:enabled="true"
android:process=":remote">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="isFromAlram" > </action>
</intent-filter>
</receiver>
答案 0 :(得分:0)
您应该添加日志记录,以查看是否在设备启动时调用了BroadcastReceiver
。使用Toast
这是一个坏主意,你可能永远不会看到它。使用日志记录并检查logcat。
警报不会通过设备启动保留。设备启动时,应调用BroadcastReceiver
。然后,您需要再次设置闹钟。