之後送日用品的通知。由於alarmManager所以我創建了一個廣播接收器來觸發BOOT_COMPLETE,仍然沒有成功重啓手機後停止。 即使沒有得到烤麪包。AlarmManager不觸發我用一個完整的alarmManager啓動時
BroadcastRreceiver類
package com.aman.dailynoti;
import...
public class BReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ((intent.getAction()).equals("android.intent.action.BOOT_COMPLETED")) {
Toast.makeText(context, "broadcast", Toast.LENGTH_SHORT).show();
SharedPreferences mpreferences=context.getSharedPreferences("myPreferences",MODE_PRIVATE);
int h=mpreferences.getInt("hour",14);
int m=mpreferences.getInt("minute", 30);
Calendar calendar= Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,h);
calendar.set(Calendar.MINUTE,m);
calendar.set(Calendar.SECOND,00);
Intent notiIntent = new Intent(context, Notification_Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, notiIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
}
AndroidManifest.xml中
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application...>
<receiver
android:name="com.aman.dailynoti.BReceiver"
android:enabled="true"
android:label="breceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
檢查你有** <使用許可權的android:NAME =「android.permission.RECEIVE_BOOT_COMPLETED」 /> **權限療法與否。 –
是的,我擁有清單。 – aman
可能重複:https://stackoverflow.com/a/20441442/2557258 – Yazon2006