1

我正在为 android 编写应用程序。我有 2 个活动布局。第一个自定义了该复选框的列表视图。用户选择列表视图项目并单击按钮。我设置了2个不同的警报。第一次用户单击第一个活动布局上的按钮并等待通知(警报与通知相结合)。通知正在发挥作用。工作:从数据库中选择某人。然后我打开了一个广播接收器并将其放入无限循环。每次随机数由数据库的长度选择。我的问题是如果我在第二个活动布局中没有问题。但是当我是第一个没有人选择并且我的数据库为空的人时。当我重新启动手机时。我的应用程序给了我异常。我该如何解决?(如果我的数据库中有 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);                            

我的广播接收器:

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) 
4

0 回答 0