Search code examples
androidandroid-intentalarmmanager

AlarmManager doesn't wake up the phone sometimes (XPERIA)


I use JellyBean, not KitKat or higher API, so onReceive method should be called at the right time. The problem is most of the times my phone is woke up, only sometimes it doesn't (I look at the time when log was saved).

This is how I create Pending Intent:

    PendingIntent pendingIntent;
    AlarmManager manager;

    Intent alarmIntent = new Intent(this, AlarmReceiver.class);

    pendingIntent = PendingIntent.getBroadcast(this, idSms, alarmIntent, 0);

    manager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);

And now BroadcastReceiver:

public class AlarmReceiver extends BroadcastReceiver {

    private static final String TAG = "AlarmReceiver";

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

So, that's it. BroadcastReceiver is declared in Manifest, as I wrote before it works most of the time, but somtimes I have to turn my phone by myself to see the log... I read few topics, I didn't find any answer why does it happen. Also I know about WakeLocker, I tried to that AFTER the log, but I checked onReceive is not even called I think, at least the log doesn't show it sometimes.

Hope for some advice or explaining why isn't it working all the time. Thank you

Edit 1: I added some code, still doesn't work when phone is sleeping:

public void onReceive(Context arg0, Intent arg1) {  
PowerManager pm = (PowerManager) arg0.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WakeLock");
wl.acquire();   
Log.d(TAG, "onReceive()");
wl.release();
}

Edit 2: changed code:

public class AlarmReceiver extends WakefulBroadcastReceiver {

    private static final String TAG = "AlarmWakefulReceiver";

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Log.d(TAG, "onReceive()");
        Intent service = new Intent(arg0, SmsService.class);
        Log.d(TAG, "Creating service");
        startWakefulService(arg0, service);
    }
}


public class SmsService extends IntentService {
    private static final String TAG = "IntentService";

    public SmsService() {
        super("SmsService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Toast.makeText(this, "Service completed", Toast.LENGTH_SHORT).show();
        Log.i(TAG, "Completed service");
        AlarmReceiver.completeWakefulIntent(intent);
    }

}

Service registered and worked, I set time on 8:47, checked the phone on 8:49 and got onReceive() that time. Logs:

01-15 08:49:04.894: D/AlarmWakefulReceiver(32576): onReceive()
01-15 08:49:04.894: D/AlarmWakefulReceiver(32576): Creating service
01-15 08:49:04.914: I/IntentService(32576): Completed service

Edit 3: I tested this on my phone: http://wptrafficanalyzer.in/blog/setting-up-alarm-using-alarmmanager-and-waking-up-screen-and-unlocking-keypad-on-alarm-goes-off-in-android/

My phone is Xperia L, system 4.2.2


Solution

  • http://commonsware.com/blog/2013/03/08/warning-xperia-z-stamina-alarmmanager.html

    I have Xperia L and checked this link... Well... My Xperia has Stamina mode. It seems that Stamina blocked SOMETIMES my wake up function.

    To solve this you need to go to power managment options, go to stamina mode and turn it off or set our application as exception. Works fine so far, I will test more and tell if there was a problem!

    Edit 1:

    10:04 - I set the alarm to be fired at 10:21, checked the effect at 10:28, works!

    Logs:

    01-15 10:21:16.804: D/AlarmWakefulReceiver(11202): onReceive()
    01-15 10:21:16.814: D/AlarmWakefulReceiver(11202): Creating service
    01-15 10:21:16.834: I/IntentService(11202): Completed service