【问题标题】:What is best to use as SetRepeating() does not work after 19 APISetRepeating() 在 19 API 之后无法正常工作,最好使用什么
【发布时间】:2018-05-08 15:01:51
【问题描述】:

我正在为以前的实习学生开发的应用程序做一些支持工作,该应用程序接受用户输入,比如 5。这意味着每隔 5 分钟就会响一次警报。

她的应用程序已返回给我,因为警报有自己的想法并且不一致。这是她正在使用的代码:

 alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
        //set alert
        alertDialogBuilder
                .setTitle("IP Check frequency: " + time.getText() + " minutes")
                .setMessage("Processing commenced at \n" + startTime.getText())
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        if (currentTime.after(alarmTime)) {
                            Toast.makeText(MainActivity.this, "Missed first alert", Toast.LENGTH_LONG).show();
                        }
                        intent1 = new Intent(MainActivity.this, MyBroadcastReceiver.class);

                        i = Integer.parseInt(time.getText().toString());
                        scTime2 = (i * 60 * 1000); //5 minutes before set time

                        pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
                        manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                        manager.setRepeating(AlarmManager.RTC_WAKEUP, timeCommenced, scTime2, pendingIntent);
                        Toast.makeText(MainActivity.this, "Alert Set", Toast.LENGTH_SHORT).show();
                        stopped.setVisibility(View.VISIBLE);
                        commenced1.setVisibility(View.GONE);

                        //Change editText to TextView
                        time.setVisibility(View.GONE);
                        timeText.setVisibility(View.VISIBLE);
                        timeText.setText(time.getText().toString());
                        processingText.setText(R.string.processing_commenced);

                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        alertDialog = alertDialogBuilder.create();
        alertDialog.show();

    }
}

我可以看到她正在使用SetRepeating(),并且我从 api 级别 19 中读到过这可能是问题所在。

我尝试过使用SetExact(),但在方法内的变量下方出现了一条红线。

谁能告诉我如何在保持变量的同时保持一致性?

谢谢!

【问题讨论】:

    标签: java android alarmmanager repeatingalarm


    【解决方案1】:

    setRepeatingsetExact 采用不同数量的变量,因此您不能只使用相同的东西而不更改代码。

    如果你想在那个确切的时间触发某些东西,那么你需要使用setExact,每次警报触发时,你都需要计算下一次它应该自动归档的时间,并再次使用setExact并为其设置一个新时间触发。

    如果您使用setRepeating,操作系统可以/将会将触发时间推迟到可以同时触发多个警报以节省电池的时间。

    【讨论】:

    • 那么在第一次触发后我将如何再次使用 setExact 呢?感谢@tyczj的回复
    • 是的,每次触发后,您都必须使用 setExact 设置新警报
    • 我应该把它放在哪里以确保它重复?如果它无限期地重复,直到它停止
    【解决方案2】:

    你是对的。根据https://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int,%20long,%20long,%20android.app.PendingIntent)

    注意:从 API 19 开始,所有重复警报都是不准确的。如果你的 应用程序需要精确的交付时间,那么它必须使用一次性 准确的警报,每次重新安排如上所述。遗产 targetSdkVersion 早于 API 19 的应用程序将 继续有他们所有的警报,包括重复警报, 被视为精确。

    他们建议您使用set() 而不是setRepeating(),后者会触发一次。 触发警报后,在处理程序或 pendingIntent 中再次调用 set()。

    【讨论】:

    • 我试过在那里使用 set() 但我仍然得到红线。从我的方法中的变量中,我可以取出哪些?
    • 你能给我一些关于如何在处理程序或我的待处理意图中再次调用 set() 的代码吗?
    猜你喜欢
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多