Search code examples
androidbroadcastreceiveralarmmanagerandroid-8.0-oreo

Alarm Repeat in Android 8


Can you explain me why this code work well on Android Lollipop but i don't work well on Android Oreo ? The code set an Alarm that start after 5 second and repeat every 4 second. It send a broadcast and MyReceive show a Toast. In android 8, it start in late and the next broadcast don't send every 4 second but in random time (in late). What I can do ?

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);
                }
            }
        });


    }


}

MyReceiver

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

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

I hope that you can help me thanks!


Solution

  • The precision of 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.