Android 启动时的启动警报不起作用
我已经创建了一个BroadcastReceiver,在那里我启动了3个警报和3个通知,但我尝试了我的代码,但它不工作,警报没有启动。 我还尝试将通知代码包括在if条件中,但当通知启动时,应用程序崩溃。 这是我的广播接收器代码:Android 启动时的启动警报不起作用,android,broadcastreceiver,android-notifications,Android,Broadcastreceiver,Android Notifications,我已经创建了一个BroadcastReceiver,在那里我启动了3个警报和3个通知,但我尝试了我的代码,但它不工作,警报没有启动。 我还尝试将通知代码包括在if条件中,但当通知启动时,应用程序崩溃。 这是我的广播接收器代码: @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_BOOT_COM
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
setAlarm(context);
}
String keyid = intent.getStringExtra("number");
String notif=PreferenceManager.getDefaultSharedPreferences(context).getString("notif", "abilitato");
if(notif.equals("abilitato")){
if(keyid.equals("0")){
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context,
0, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context);
mBuilder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("E' ora di colazione!")
.setContentText("Cosa c'è per colazione?")
.setSound(alarmSound)
.setLights(Color.BLUE, 1000, 500)
.setVibrate(new long[] { 1000, 1000})
.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
if(keyid.equals("1")){
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context,
1, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder2 =
new NotificationCompat.Builder(context);
mBuilder2.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("E' ora pranzo!")
.setContentText("Cosa c'è per pranzo?")
.setSound(alarmSound)
.setLights(Color.BLUE, 1000, 500)
.setVibrate(new long[] { 1000, 1000})
.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder2.build());
}
if(keyid.equals("2")){
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context,
2, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder3 =
new NotificationCompat.Builder(context);
mBuilder3.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("E' ora cena!")
.setContentText("Cosa c'è per cena?")
.setSound(alarmSound)
.setLights(Color.BLUE, 1000, 500)
.setVibrate(new long[] { 1000, 1000})
.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder3.build());
}
}
}
public void setAlarm(Context context){
String breakfast1=PreferenceManager.getDefaultSharedPreferences(context).getString("breakfast", "abilitato");
String lunch1=PreferenceManager.getDefaultSharedPreferences(context).getString("lunch", "abilitato");
String dinner1=PreferenceManager.getDefaultSharedPreferences(context).getString("dinner", "abilitato");
Database d;
AlarmManager alarmManager;
PendingIntent pendingIntent;
d=new Database(context);
String dataString=d.checkDataString();
String n;
//COLAZIONE
int duepunti = breakfast1.indexOf(":");
String hourColazione = breakfast1.substring(0,duepunti);
int intHourColazione=Integer.parseInt(hourColazione);
String minuteColazione = breakfast1.substring(duepunti + 1);
int intMinuteColazione=Integer.parseInt(minuteColazione);
int start = dataString.indexOf("/");
String dayString = dataString.substring(0,start);
String monthString = dataString.substring(start + 1,start+3);
String yearString = dataString.substring(start+4);
int day = Integer.parseInt(dayString);
int month = Integer.parseInt(monthString);
int year = Integer.parseInt(yearString);
Calendar calNow = Calendar.getInstance();
Calendar c = (Calendar) calNow.clone();
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month-1);
c.set(Calendar.DAY_OF_MONTH, day);
c.set(Calendar.HOUR_OF_DAY, intHourColazione);
c.set(Calendar.MINUTE, intMinuteColazione);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
if(c.after(System.currentTimeMillis())){
c.add(Calendar.DAY_OF_MONTH, 1);
}
Intent myIntent = new Intent(context, MyReceiver.class);
n="0";
myIntent.putExtra("number", n);
if(PendingIntent.getBroadcast(context, 0,
myIntent,
PendingIntent.FLAG_NO_CREATE) == null){
pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent,0);
alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
//PRANZO
int duepunti2 = lunch1.indexOf(":");
String hourPranzo = lunch1.substring(0,duepunti2);
int intHourPranzo=Integer.parseInt(hourPranzo);
String minutePranzo = lunch1.substring(duepunti2 + 1);
int intMinutePranzo=Integer.parseInt(minutePranzo);
Calendar calNow2 = Calendar.getInstance();
Calendar c2 = (Calendar) calNow2.clone();
c2.setTimeInMillis(System.currentTimeMillis());
c2.set(Calendar.YEAR, year);
c2.set(Calendar.MONTH, month-1);
c2.set(Calendar.DAY_OF_MONTH, day);
c2.set(Calendar.HOUR_OF_DAY, intHourPranzo);
c2.set(Calendar.MINUTE, intMinutePranzo);
c2.set(Calendar.SECOND, 0);
c2.set(Calendar.MILLISECOND, 0);
if(c2.after(System.currentTimeMillis())){
c2.add(Calendar.DAY_OF_MONTH, 1);
}
Intent myIntent2 = new Intent(context, MyReceiver.class);
n="1";
myIntent2.putExtra("number", n);
if(PendingIntent.getBroadcast(context, 1,
myIntent2,
PendingIntent.FLAG_NO_CREATE) == null){
pendingIntent = PendingIntent.getBroadcast(context, 1, myIntent2 ,0);
alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
//CENA
int duepunti3 = dinner1.indexOf(":");
String hourCena = dinner1.substring(0,duepunti3);
int intHourCena=Integer.parseInt(hourCena);
String minuteCena = dinner1.substring(duepunti3 + 1);
int intMinuteCena=Integer.parseInt(minuteCena);
Calendar calNow3 = Calendar.getInstance();
Calendar c3 = (Calendar) calNow3.clone();
c3.setTimeInMillis(System.currentTimeMillis());
c3.set(Calendar.YEAR, year);
c3.set(Calendar.MONTH, month-1);
c3.set(Calendar.DAY_OF_MONTH, day);
c3.set(Calendar.HOUR_OF_DAY, intHourCena);
c3.set(Calendar.MINUTE, intMinuteCena);
c3.set(Calendar.SECOND, 0);
c3.set(Calendar.MILLISECOND, 0);
if(c.after(System.currentTimeMillis())){
c.add(Calendar.DAY_OF_MONTH, 1);
}
Intent myIntent3 = new Intent(context, MyReceiver.class);
n="2";
myIntent3.putExtra("number", n);
if(PendingIntent.getBroadcast(context, 2,
myIntent3,
PendingIntent.FLAG_NO_CREATE) == null){
pendingIntent = PendingIntent.getBroadcast(context, 2, myIntent3,0);
alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c3.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
}
在舱单中:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
...
<receiver android:name="com.lp.lemiediete.MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
您正在onReceive中调用getStringExtranumber。该意图没有额外的kety编号,因此将返回null。然后,您将在此处得到一个NullPointerException:
在以下代码中调用setAlarm后,可能需要添加一个return语句:
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
setAlarm(context);
}
你登记了你的接受者了吗?请参阅以下链接:。Quoted:您可以使用Context.registerReceiver动态注册此类的实例,也可以通过AndroidManifest.xml中的标记静态发布实现。也许你可以发布你的AndroidManifest.xmlaswell@DonMushroom谢谢你的回复,我已经发布了我的清单代码似乎是正确的。如果您可以在应用程序崩溃后包含包含错误消息的日志猫,将非常有用。如果你能以一种更易读的方式格式化你的代码,那么也会更容易发现你的问题@Don蘑菇还发布日志:引导接收器是否返回意图数据?关键数据是否来自引导接收器?我不这么认为。请你看那条线。String keyid=intent.getStringExtraNumberTank的回复David,你为什么说intent没有带有密钥号的ann extra,我使用:myIntent.putetraNumber,n;在setAlarm方法中,是否不正确?在启动完成时调用MyReceiver。这个意图不会有额外的数字。我明白,但我怎么能在BOOT_COMPLETE Intent上加一个额外的数字呢?你不能。当系统启动时,Android会广播这个意图。该意图由在其清单中注册的所有广播接收者接收。如果您在引导完成时需要一些信息,您应该将这些信息存储在应用程序的共享首选项中。然后,当您收到引导完成意图时,您可以查看共享首选项并检索必要的数据。我希望这是清楚的。
if(keyid.equals("0")){
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
setAlarm(context);
}