Android setRepeating () and setInexactRepeating () do not work

I did not find a solution to this absurd problem. setRepeating () and setInexactRepeating () do not work at all. I tried both of these methods. Below is my code:

Intent intentService = new Intent(context, ServiceReceiver.class);
intentService.putExtra("checkStrikeAndNews", true);
PendingIntent pendingIntentSN = PendingIntent.getBroadcast(context,   0,intentService, PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

//scheduling checkNews
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntentSN);

I also tried using setInexactRepeating () and it does not work. I also used ELAPSED_REALTIME_WAKEUP and SystemClock.elapsedRealtime (), the same behavior.

The ServiceReceiver.class broadcast receiver works fine, it receives other alarm intentions and intentions (AlarmManager set () and setExact ()) without any problems. I checked the code on ICS, JellyBean and Marshmallow.

Many thanks!

+4
source share
1 answer
  • I think you should use PendingIntent.getService()insteadPendingIntent.getBroadcast()

  • , ​​ Manifest.xml

  • setInexactRepeating()

App.class,

public class App extends Application {

    private static final int INTERVAL = 1000 * 60;
    private static final int DELAY = 5000;

    @Override
    public void onCreate() {
        super.onCreate();
        setupAlarm();
    }

    private void setupAlarm() {
        PendingIntent myPendingIntent = PendingIntent.getService(this, 0, MyService.newIntent(this), 0);
        AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, DELAY, INTERVAL, myPendingIntent);
    }
}

MyService.class,

public class MyService extends IntentService {

    private static final String TAG = MyService.class.getSimpleName();
    private static final String EXTRA_CHECK = "checkStrikeAndNews";

    /**
     * Creates an IntentService.  Invoked by your subclass constructor.
     */
    public MyService() {
        super("MyService");
    }

    public static Intent newIntent(Context context) {
        return new Intent(context, MyService.class) //
                .putExtra(EXTRA_CHECK, true);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent.getBooleanExtra(EXTRA_CHECK, false)) {
            Log.i(TAG, "I just received an intent");
        }
    }
}

Manifest.xml

<manifest
    package="com.darzul.test"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:name=".App"
        android:theme="@style/AppTheme">

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

        <service android:name=".service.MyService" />
    </application>

</manifest>

01/02/2015

BroadcastReceiver

App.class

public class App extends Application {

    private static final int INTERVAL = 1000 * 60;
    private static final int DELAY = 5000;

    @Override
    public void onCreate() {
        super.onCreate();
        setupAlarm();
    }

    private void setupAlarm() {
        Intent myIntent = new Intent(this, MyReceiver.class);
        PendingIntent myPendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0);
        AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, DELAY, INTERVAL, myPendingIntent);
    }
}

: SDK , 5.1, 1 .

60000 Android 5.1; , . API 22, AlarmManager , 5 , 60 . 5 , .

MyReceiver.class

public class MyReceiver extends BroadcastReceiver {

    private static final String TAG = "MyReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive");
    }
}

Manifest.xml

<manifest
    package="com.darzul.test"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:name=".App"
        android:theme="@style/AppTheme">

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

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

</manifest>
+8

Source: https://habr.com/ru/post/1611085/


All Articles