我想在设备启动时启动警报,因为我已经完成了以下工作
1) 用户权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2)在清单文件中添加带有意图操作的接收器
<receiver
android:name=".sms.BootReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
3) 来源
public class BootReceiver extends BroadcastReceiver {
private AlarmManager dayAlarmMgr;
private PendingIntent dayAlarmIntent;
private NotificationManager mNotificationManager;
private NotificationCompat.Builder builder;
private Context context;
public static final int NOTIFICATION_ID = 2;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Toast.makeText(context, "FIRED BOOT COMPLETE" , Toast.LENGTH_LONG).show();
}
}
}
上面的代码在genymotion中工作,但不在真实设备上