简体   繁体   English

重复提醒通知Android(java)问题

[英]Repeat reminder notification Android (java) problem

I created a reminder every 10 minutes 00 seconds, but notifications always come at different times... for example 5:10:32, 5:20:41, 5:32:01, 5:41:20.. instead of 5: 10:00, 5:20:00, 5:30:00, 5:40:00 Can anyone know what the problem might be?我每 10 分 00 秒创建一个提醒,但通知总是在不同的时间出现……例如 5:10:32、5:20:41、5:32:01、5:41:20 .. 而不是 5 : 10:00, 5:20:00, 5:30:00, 5:40:00 谁能知道可能是什么问题?

public class MainActivity extends AppCompatActivity  implements View.OnClickListener {

    private int notificationId = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.setBtn).setOnClickListener(this);
        findViewById(R.id.cancelBtn).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        EditText editText = findViewById(R.id.editText);
        TimePicker timePicker = findViewById(R.id.timePicker);

        // Set notificationId & message
        Intent intent = new Intent(MainActivity.this, AlarmRecevier.class);
        intent.putExtra("notificationId", notificationId);
        intent.putExtra("message", editText.getText().toString());

        //PendingIntent
        PendingIntent alarmIntent = PendingIntent.getBroadcast(
                MainActivity.this, 0,intent,PendingIntent.FLAG_UPDATE_CURRENT); //FLAG_CANCEL_CURRENT
        //AlarmManager
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        switch (v.getId()){
            case R.id.setBtn:
                //set Alarm
                int hour = timePicker.getHour();
                int minute = timePicker.getMinute();
                // Create time
                Calendar startTime = Calendar.getInstance();
                startTime.set(Calendar.HOUR_OF_DAY, hour);
                startTime.set(Calendar.MINUTE, minute);
                startTime.set(Calendar.SECOND, 0);
                long alarmStartTime = startTime.getTimeInMillis();
                long repTime = 600000L;

                // Set Alarm Repeat
                //alarmManager.set(AlarmManager.RTC_WAKEUP, alarmStartTime, alarmIntent);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmStartTime, repTime, alarmIntent);
                Toast.makeText(this,"Done!", Toast.LENGTH_SHORT).show();
                break;


            case R.id.cancelBtn:
                //CancelAlarm
                alarmManager.cancel(alarmIntent);
                Toast.makeText(this,"Cancelled", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
public class AlarmRecevier extends BroadcastReceiver {

    private static final String CHANNEL_ID = "SAMPLE_CHANNEL";

    @Override
    public void onReceive(Context context, Intent intent) {
        // get ID & message from Intent
        int notificationId = intent.getIntExtra("notificationId",0);
        String message = intent.getStringExtra("message");

        //Call MainActivity when notification is tapped
        Intent mainIntent = new Intent(context, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(
                context,0,mainIntent,PendingIntent.FLAG_UPDATE_CURRENT
        );

        //NotificationManager
        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            //For API 26 and above.
            CharSequence channel_name = "My Notification";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;

            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, channel_name,importance);
            notificationManager.createNotificationChannel(channel);
        }
        // prepare notification
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_flower)
                .setContentTitle("FLOVERS")
                .setContentText("Полей цветок")
                .setContentIntent(contentIntent)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setAutoCancel(true);

        //Notify
        notificationManager.notify(notificationId, builder.build());


    }
}

From the documentation of setRepeating() :setRepeating()的文档中:

Note: as of API 19, all repeating alarms are inexact.注意:自 API 19 起,所有重复警报均不准确。 If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above.如果您的应用程序需要精确的交付时间,那么它必须使用一次性精确警报,并如上所述重新安排每次。 Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact. targetSdkVersion早于 API 19 的旧版应用程序将继续将其所有警报(包括重复警报)视为准确。

They make it difficult because they want to discourage precise alarms.他们让事情变得困难,因为他们想阻止精确的警报。 Precise alarms drain batteries faster.精确的警报会更快地耗尽电池。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM