我正在为android编写应用程序。我有2个活动布局。第一个有自定义列表视图,checkbox.User选择listview项目并点击按钮。我设置了2个不同的闹钟。第一次用户点击第一个活动的布局和等待通知按钮(警报与通知结合)。通知正在制作。工作:从数据库中选择某人。然后我打开了一个broadcastReceiver,我把它放在无限循环中。每次都是随机数由数据库的长度选择。我的问题是没有问题我是在第二个活动布局。但是,当我第一个没人选择,我的数据库是空的。我重启手机的时候。我的应用程序给了我异常。我该如何修复它? (İf我的数据库中有4个或更多数据。我的应用程序显示第二个活动。当应用程序打开时。
我的第一个Activitiy第一个警报设置:
Intent intent = new Intent(MainActivity.this, Alarm.class);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 30);
if(calendar.before(Calendar.getInstance())) {
calendar.add(Calendar.DATE, 1);
}
PendingIntent pendingIntent = PendingIntent.getBroadcast(
MainActivity.this, 234, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() , pendingIntent);
我的BroadcastReceiver:
public class Alarm extends BroadcastReceiver {
DatabaseHelper mDatabaseHelper;
ArrayList listData = new ArrayList<>();
ArrayList listDataoran = new ArrayList<>();
ArrayList listDataBoya = new ArrayList<>();
ArrayList listDataId = new ArrayList<>();
ArrayList listDatanumber = new ArrayList<>();
Random secilmiskisi = new Random();
int sayı;
int secilmissayı;
@Override
public void onReceive(Context context, Intent intent) {
mDatabaseHelper = new DatabaseHelper(context);
Cursor data = mDatabaseHelper.getData();
while (data.moveToNext()) {
listDataId.add(data.getString(0));
listData.add(data.getString(1));
listDatanumber.add(data.getString(2));
listDataoran.add(data.getString(3));
listDataBoya.add(data.getString(4));
}
sayı = data.getCount();
if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
cancelAlarm(context);
setalarm(context);
}
cancelAlarm(context);
setalarm(context);
secilmissayı = secilmiskisi.nextInt(listDataId.size()) + 0;
while (listDataBoya.get(secilmissayı).equals("#FED804") || listDataBoya.get(secilmissayı).equals("#121210")) {
secilmissayı = secilmiskisi.nextInt(listDataId.size()) + 0;
}
}
}
根据数据文字通知显示自己:
if (listDataBoya.get(secilmissayı).equals("#1eac02") ) {
Intent anasayfa = new Intent(context, AnaMenu.class);
PendingIntent pIntentiki = PendingIntent.getActivity(context, 0, anasayfa, 0);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Intent intenttt = new Intent(context,AnaMenu.class);
Notification noti = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
noti = new Notification.Builder(context)
.setTicker("Sosyal Alarm size bir bildirim yolladı.")
.setContentTitle("Sosyal Alarm")
.setContentText(listData.get(secilmissayı) + " araman için seni bekliyor.")
.setSmallIcon(R.drawable.alarm_24dp)
.setColor(Color.YELLOW)
.setContentIntent(pIntentiki).getNotification();
}
// .setTicker(" size bir bildirim yolladı.").setSmallIcon(R.drawable.familyiiii)
// .setSubText("Bu mesaj ile artık" + isimler.get(random) + " arayacağın bir görevin oldu.")
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
//flag auto cancel.
noti.flags |= Notification.FLAG_AUTO_CANCEL;
// noti.defaults |= Notification.DEFAULT_ALL;
// noti.defaults |= Notification.DEFAULT_VIBRATE;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
Boolean vibrate = prefs.getBoolean("vibrate", true);
Boolean sound = prefs.getBoolean("sound", true);
if (vibrate) {
noti.defaults |= Notification.DEFAULT_VIBRATE;
} else {
}
if (sound) {
noti.defaults |= Notification.DEFAULT_SOUND;
} else {
}
//
notificationManager.notify(0, noti);
// setalarm(context);
}
设置警报方法:
private void setalarm(Context context) {
AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
int delay = 1000 * 2;
Random r = new Random();
int ri = r.nextInt(25 - 20) + 20;
switch (ri) {
case 20:
delay = 1000 * 60 * 60 * 20;
break;
case 21:
delay = 1000 * 60 * 60 * 21;
break;
case 22:
delay = 1000 * 60 * 60 * 22;
break;
case 23:
delay = 1000 * 60 * 60 * 23;
break;
case 24:
delay = 1000 * 60 * 60 * 24;
break;
}
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay, pi);
}
取消报警方式:
private void cancelAlarm(Context context) {
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 234, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.cancel(sender);
}
我的logcat。当我重新启动手机时:
3-30 17:06:03.508 13015-13015/something.about.berkay.sosyalalarm E/AndroidRuntime: FATAL EXCEPTION: main
Process: something.about.berkay.sosyalalarm, PID: 13015
java.lang.RuntimeException: Unable to start receiver something.about.berkay.sosyalalarm.Alarm: java.lang.IllegalArgumentException: n must be positive
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3369)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1726)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Random.java:391)
at something.about.berkay.sosyalalarm.Alarm.onReceive(Alarm.java:79)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3362)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1726)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)