我已在特定时间设置闹钟。如果手机关机,闹钟就会丢失?我已关闭手机,再次启动它,但闹铃没有在指定时间触发。
PendingIntent pendingIntent = PendingIntent.getBroadcast(
Global.a.getApplicationContext(), (int) cd.e.alarmTime, intent, 0);
AlarmManager alarmManager = (AlarmManager) Global.a.getSystemService(Activity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, cd.e.alarmTime, pendingIntent);
答案 0 :(得分:8)
首先,你需要创建一个BroadcastReceiver
来触发BOOT_COMPLETE
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent mServiceIntent = new Intent();
mServiceIntent
.setAction("com.crossfire.appname.service.BootService");
ComponentName service = context.startService(mServiceIntent);
if (null == service) {
// something really wrong here
Log.e(TAG, "Could not start service ");
}
} else {
Log.e(TAG, "Received unexpected intent " + intent.toString());
}
}
}
然后创建一个服务以重置所有警报:
public class BootService extends Service {
private final String TAG = "BootService";
@Override
public IBinder onBind(final Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Trace.i(TAG, "created");
}
@Override
public void onStart(final Intent intent, final int startId) {
super.onStart(intent, startId);
Trace.i(TAG, "started");
LoadAlarmsFromDatabase();
}
private void LoadAlarmsFromDatabase() {
Cursor c = mDbHelper.getPendingAlarms();
if (c.moveToFirst()) {
do {
int id = c.getInt(mDbHelper.ID_COLUMN);
String time = c.getString(mDbHelper.TIME_COLUMN);
addNotification(id, time);
} while (c.moveToNext());
}
c.close();
}
private void addNotification(int apptId, String time) {
Trace.d(TAG, "addNotification " + apptId + ", " + time);
Intent intent = new Intent(BootService.this,
ApptBroadcastReceiver.class);
intent.putExtra("appt_id", Integer.toString(apptId));
PendingIntent mAlarmSender = PendingIntent.getBroadcast(
BootService.this, apptId, intent, 0);
long alarmTime = System.currentTimeMillis() + 60000;
Calendar c = null;
try {
c = CustomDateUtils.StringToCalendar(time);
alarmTime = c.getTimeInMillis();
} catch (ParseException e) {
Log.e(TAG, "ParseException");
}
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, alarmTime, mAlarmSender);
}
}
最后,将权限和接收者添加到清单
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".receiver.BootReceiver" android:enabled="true">
<intent-filter>
<action android:name ="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
答案 1 :(得分:4)
尝试 @Woodsy 方法,但没有工作,所以我修了一些东西,以便它可以工作
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Log.d("receiver", "The Receiver is called");
Intent mServiceIntent = new Intent(context, BootService.class);
context.startService(mServiceIntent);
} else {
Log.d("boot receiver ",
"Something went wrong with the boot complete maybe with permissions ");
}
}
}
* 权限&amp;清单*
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Booting update
<service android:name="com.packagename.appname.service.BootService" />
<receiver
android:name=".Receivers.BootReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
希望这可以帮助其他人
答案 2 :(得分:0)
你可以这样做:
将时间存储在SharedPreference
,抓住Intent.ACTION_BOOT_COMPLETED
,然后再次设置AlarmManager
。