Alarm Does Not Wake up my Service
Asked Answered
P

1

2

I have the following code, I expect that this alarm invokes my Service regardless of the state of the phone. Even if its in sleep mode, I need it to access the internet and make some network calls.

Why doesn't it work when the phone is in sleep mode ?

Alarm Manager

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 5);
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent notifyintent = new Intent(this, OnAlarmReceiver.class);
        notifyintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        notifyintent.setAction("android.intent.action.NOTIFY");
        PendingIntent notifysender = PendingIntent.getBroadcast(this, 0, notifyintent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 20 * 1000,
                notifysender);

AlarmReceiver

public class OnAlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // PullPendingRequests.acquireStaticLock(context)
        context.startService(new Intent(context, PullPendingRequests.class));
    }
}

PullPendingRequests, PendingIntent

public class PullPendingRequests extends IntentService 

public PullPendingRequests() {
        super("PullPendingRequests");
        me = this ;
    }

@Override
    final protected void onHandleIntent(Intent intent) {

        Location myLocation;
        Log.d("Taxeeta:PullPendingRequets", "Started Location");
        if (God.locationManager == null)
            God.locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        myLocation = God.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (myLocation != null)
            onLocationChanged(myLocation);
        else {
            God.notifications.setSpeedNotification();
        }

        Log.d("Taxeeta:PullPendingRequets", "Ended Location");
    }

Manifest for permissions and other settings

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_LOGS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <!-- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> -->
 <service
        android:name="com.taxeeta.PullPendingRequests"
        android:enabled="true"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Light.NoTitleBar" />

<receiver
            android:name="com.taxeeta.support.OnAlarmReceiver"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.NOTIFY" />
            </intent-filter>
        </receiver>

Edit : Should this work, it isn't for me ?

public class PullPendingRequests extends IntentService implements LocationListener {

    private static final String NAME = "com.commonsware.cwac.wakeful.WakefulIntentService";
    private static volatile PowerManager.WakeLock lockStatic = null;
    private static PowerManager.WakeLock lock;

    synchronized private static PowerManager.WakeLock getLock(Context context) {
        if (lockStatic == null) {
            PowerManager mgr = (PowerManager) context.getSystemService(Context.POWER_SERVICE);

            lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NAME);
            lockStatic.setReferenceCounted(true);
        }

        return (lockStatic);
    }

    @Override
    final protected void onHandleIntent(Intent intent) {
        try {
            lock = getLock(this.getApplicationContext());
            lock.acquire();
            Location myLocation;
            Log.d("Taxeeta:PullPendingRequets", "Started Location");
            if (God.locationManager == null)
                God.locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            myLocation = God.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (myLocation != null)
                onLocationChanged(myLocation);
            else {
                God.notifications.setSpeedNotification();
            }

            Log.d("Taxeeta:PullPendingRequets", "Ended Location");
        } finally {
            if (lock.isHeld()) {
                lock.release();
            }
        }
    }
Prototype answered 9/4, 2013 at 14:46 Comment(0)
R
6

Why doesn't it work when the phone is in sleep mode ?

Because startService() is asynchronous, and Android only guarantees that a _WAKEUP alarm will keep the device awake until onReceive() ends. You will need to use a WakeLock, either directly or by means of using my WakefulIntentService.

Riana answered 9/4, 2013 at 14:50 Comment(2)
Thanks, you are awesome !! I have just added a possible fix, but it isn't working. Did I miss something ?Prototype
@Siddharth: "Did I miss something ?" -- yes. You cannot wait until onHandleIntent() to obtain the lock. That occurs well after onReceive() returns, and the device may fall asleep before then.Riana

© 2022 - 2024 — McMap. All rights reserved.