为什么30-30%的Android用户无法使用闹钟?

时间:2019-01-10 19:13:35

标签: android alarmmanager

我目前正在尝试进行一项研究,以提示参与者使用他们的Android设备每天六次报告信息,持续五天。为此,我在第一次打开应用程序时安排了30条警报。我的代码可以在我的设备(运行Android 8.1的Nexus 6p)上完美运行,并且似乎可以为60%至70%的参与者完美运行。但是,对于另外30-40%的参与者,这些参与者从未收到过警报(最典型的警报),或者警报停止出现在研究的一两天之内。

首先,如果需要,我为警报创建一个频道

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "my_channel_01";
        String description = "my_channel_01";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("my_channel_01", name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

接下来,我将希望将警报发送的所有30个日期附加到AlarmDates列表中,然后遍历该列表并为每个日期设置一个警报。

public void startAlert(){

    for (int i = 0; i < alarmDates.size(); i++){

        int request_code = 234324243;

        request_code += i;

        String string = alarmDates.get(i);
        DateFormat format = new SimpleDateFormat("MMMM d, yyyy HH:mm:ss");
        Date date = new Date();
        try {
            date = format.parse(string);
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Intent intent = new Intent(this, MyBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                this.getApplicationContext(), request_code, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        if (Build.VERSION.SDK_INT >= 23) {
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
                    date.getTime(), pendingIntent);
        } else if (Build.VERSION.SDK_INT >= 19) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);
        }
    }

我的广播接收器如下:

public class MyBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // Create the notification to be shown



    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context, "my_channel_01")
                    //.setSmallIcon(R.drawable.icon)
                    .setSmallIcon(R.drawable.ic_stat_name)
                    .setContentTitle("How are things going?")
                    .setContentText("Tap here to tell us how you're feeling")
                    .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                    .setAutoCancel(true)
                    .setPriority(2);


    Intent resultIntent = new Intent(context, MainActivity2.class);

    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    context,
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );

    mBuilder.setContentIntent(resultPendingIntent);

    Random rand = new Random();

    int n = rand.nextInt(5000) + 1;

    // Sets an ID for the notification
    int mNotificationId = n;
    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr =
            (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

    // Make sure any previously launched alarms are removed
    mNotifyMgr.cancelAll();
    // Builds the notification and issues it.
    mNotifyMgr.notify(mNotificationId, mBuilder.build());

}

我目前正在与三个参与者一起解决这个问题。他们使用的是三星Galaxy J7,LG Tribute Dynasty和Galaxy s9 +。他们都没有报告看到我的应用发出了任何通知。

非常感谢您的帮助。

0 个答案:

没有答案