Android AlarmManager stops when activity die

2019-02-04 07:24发布

I start AlarmManager with PendingIntent and on few phones Alarm is not responding. On some devices is working ok on others it fails. I have made a few tests on different phones.

Nexus works ok, also Samsung Galaxy S4 zoom (4.2) works ok.

Samsung note 2 (4.3) works ok.

OPPO (4.4.4) alarm dies.

I have also implemented broadcast receivers which are working as they should on all devices.

    Log.v(TAG, "START ALARM");

    Intent intentAlarm = new Intent(context, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 1000, 5000, pendingIntent);

9条回答
等我变得足够好
2楼-- · 2019-02-04 07:50

Try the following:

1) Add teh Wake_lock permission to your manifest.

<uses-permission android:name="android.permission.WAKE_LOCK">

2) Change

alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 1000, 5000, pendingIntent);

with

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000, 5000, pendingIntent);
查看更多
Deceive 欺骗
3楼-- · 2019-02-04 07:51

i have also used Alarm Service in my project for preparative task in very 6 or 7 minutes. And it is running fine in all phone.

i have make a alarm Service like this:

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;

public class MyAlarmService {
    private static PendingIntent resetAlarm;
    private static String TAG="CellPoliceChildGPSAlarmService";
    private static AlarmManager am;
    public static void start(Context context) {
        try {
            // We want the alarm to go off 30 seconds from now.
            long firstTime = SystemClock.elapsedRealtime();

            // Create an IntentSender that will launch our service, to     be scheduled with the alarm manager.
            //resetAlarm = PendingIntent.getService(context, 0, new     Intent(context, Get_NonRootDetails.class), 0);
            resetAlarm = PendingIntent.getService(context, 0, new     Intent(context, CallNonRBackgroundService.class), 0);
            // Schedule the alarm!
            am = (AlarmManager)     context.getSystemService(Context.ALARM_SERVICE);
            Log.i(TAG, firstTime+"");
            am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,     firstTime, 10*1000*60, resetAlarm);
        } 
        catch (Exception e) {
            Log.v("CellInfo", "Exception while start the     MyAlarmService at: " + e.getMessage());
        } 
    }

    public static void stop(Context context) {
        try {
            // When interval going to change from web services
            am.cancel(resetAlarm);
        } 
        catch (Exception e) {
            Log.v("CellInfo", "Exception while start the     MyAlarmService at: " + e.getMessage());
        }
    }


}

I have called or start like this;

MyAlarmService.start(SplashActivity.this);

Given permission in Manifest:

<uses-permission android:name="android.permission.WAKE_LOCK">

    <service
            android:name="com.secure.DataCountService"
            android:enabled="true" >
            <intent-filter>
                <action     android:name="com.secure.MyService" />
            </intent-filter>
        </service>

For notifications i also used pending intents like;

    Intent notificationIntent = new Intent(context, DashBoardActivity.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(context,     0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle,     PushNotificationUtils.notiMsg, pendingIntent);
        notification.flags  |=  notification.FLAG_AUTO_CANCEL;
查看更多
Ridiculous、
4楼-- · 2019-02-04 07:53
Try this it works when activity is not running..

        Calendar calendar = Calendar.getInstance();

        long timemills = calendar.getTimeInMillis();
        Intent myIntent = new Intent(this, TimeChangeReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

            alarmManager.set(AlarmManager.RTC, timemills, pendingIntent);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timemills,
                    10000, pendingIntent);
查看更多
登录 后发表回答