警报重复 Android 8

Alarm Repeat in Android 8

你能解释一下为什么这段代码在 Android Lollipop 上运行良好但我在 Android Oreo 上运行不佳吗?该代码设置了一个警报,该警报在 5 秒后开始并每 4 秒重复一次。它发送广播,MyReceive 显示 Toast。在 android 8 中,它开始晚了,下一个广播不是每 4 秒发送一次,而是随机时间(晚)发送。我能做什么?

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {



    private final String LOG_TEST = "LOG_TEST";
    private Button bStart, bStop;
    private Intent intent;
    private PendingIntent pendingIntent;
    public static final String MY_CUSTOM_ACTION = "mio.broadcast";

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

        final AlarmManager alarm = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        intent = new Intent(MainActivity.this, MyReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0,intent,0);

        bStart=(Button)findViewById(R.id.bStart);
        bStop= (Button)findViewById(R.id.bStop);


        bStart.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                Calendar attivazione = Calendar.getInstance();

                alarm.setRepeating(AlarmManager.RTC_WAKEUP, attivazione.getTimeInMillis()+5000,4000,pendingIntent);
            }
        });

        bStop.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                if(alarm!=null){
                    alarm.cancel(pendingIntent);
                }
            }
        });


    }


}

我的收件人

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Vibrazione inserita", Toast.LENGTH_LONG).show();
    }
}

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.redwi.alerttry">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="false" />
    </application>

</manifest>

希望大家能帮帮我谢谢!

Can you explain me why this code work well on Android Lollipop but i don't work well on Android Oreo ?

AlarmManager 不是为经常做的事情而设计的。注意:

  • 在Android 4.4+,setRepeating()将不准确,这意味着警报可以在

  • 期间的任何地方发生
  • 在 Android 5.1+ 上,setRepeating() 将被限制为至少一分钟

  • 在 Android 6.0+ 上,您的闹钟会受到打瞌睡模式的影响,可能还会受到应用待机的影响,导致它们 运行 在设备不在充电器不动

What I can do ?

如果您希望每隔几秒从前台显示一个 Toast,请使用 postDelayed():

/***
  Copyright (c) 2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  Covered in detail in the book _The Busy Coder's Guide to Android Development_
    https://commonsware.com/Android
 */

package com.commonsware.android.post;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class PostDelayedDemo extends Activity implements Runnable {
  private static final int PERIOD=5000;
  private View root=null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    root=findViewById(android.R.id.content);
  }

  @Override
  public void onStart() {
    super.onStart();

    run();
  }

  @Override
  public void onStop() {
    root.removeCallbacks(this);

    super.onStop();
  }

  @Override
  public void run() {
    Toast.makeText(PostDelayedDemo.this, "Who-hoo!", Toast.LENGTH_SHORT)
         .show();
    root.postDelayed(this, PERIOD);
  }
}

(来自 this sample project

setRepeating is not guaranteed to be exact and could vary from device to device unpredictably. If you need more precise scheduling, use setExact and reschedule the alarm yourself. From the docs的精度:

Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.