Search code examples
androidbroadcastreceiverandroid-servicewakelockandroid-wake-lock

WAKE_LOCK won't work


I'm working on an App which is supposed to open a link to some music which will be played automatically through an app. This works fine when the screen isn't locked! However, this just won't work while the screen is locked and sleeping (it should be activated through an alarm and the music app normally doesn't have any problems to play music while the phone is sleeping). So I have imported the PowerManager:

   import android.os.PowerManager;

In my public class I have:

   private PowerManager.WakeLock mWakeLog;

And this code:

   PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
   mWakeLog = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "stuff");
   mWakeLog.acquire();

Then the link is supposed to open and later:

   mWakeLog.release();

I tested through logging that the alarm is actually activating this code but the music just won't play. However, it will play the music immediately when I unlock my phone, but that's not what I want.

Now this is interesting: the music will play if the screen is locked but waked!

Of cause I have added the permissions:

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

I also tried some other flags like "FULL_WAKE_LOCK" and such and I also tried with this. The alarm is calling this class and in it's onCreate I've got this:

   getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
   //some code
   getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

or

   v.setKeepScreenOn(true);

So it appears that the main problem might be that the wakelock doesn't turn on the screen with for example "FULL_WAKE_LOCK".


UPDATE #2.1

So on a suggestion from Richard Le Mesurier and galex and also around other sites, I'm trying this whole stuff right now with WakefulBroadcastReceiver but here comes the next problem: my receiver just won't start. So here is the code in the MainActivity when the alarm gets startet:

    Intent intent = new Intent(MainActivity.this, AlarmReceiverActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000,   pendingIntent);

And in my AlarmReceiverActivity onCreate I'm calling the Broadcast

    Intent intent = new Intent(AlarmReceiverActivity.this, MyWakefulReceiver.class);
    sendBroadcast(intent);

And in the MyWakefulReceiver I got this:

    public class MyWakefulReceiver extends WakefulBroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(MyWakefulReceiver.class.getSimpleName(), "received");
            Intent service = new Intent(context, MyIntentService.class);
            startWakefulService(context, service);
        }
    }

Unfortunately it's NOT logging "received" :c

Now here's my service:

       public class MyIntentService extends IntentService {
           public MyIntentService() { super("MyIntentService"); }
           @Override
           protected void onHandleIntent(Intent intent) {

               //bla code
               Log.d(MyIntentService.class.getSimpleName(), "service");

               MyWakefulReceiver.completeWakefulIntent(intent);
           }
       }

And it's not logging "service"

Of course I have updated my manifest:

      <service android:name=".MyIntentService" android:enabled="true"></service>
      <receiver android:name=".MyWakefulReceiver"></receiver>

Solution

  • Ok I solved this, I guess. It appears that the main problem was that there was no activity which the WakeLock could show when it wakes my phone. I created an activity to show but the link that got opened somehow unfocused my activity. But I didn't even want to have such an activity in the first place so I wrote an onPause() method which will finish the activity. Thanks for your help :P