1

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>
halfer
  • 19,824
  • 17
  • 99
  • 186
Loominarteyy
  • 31
  • 1
  • 8
  • For this expermient, I'm using spotify – Loominarteyy Jul 30 '15 at 09:30
  • just a link that will be opened with this app on default. It works when the screen is locked but not sleeping so I don't think the problem is the music app... – Loominarteyy Jul 30 '15 at 10:33
  • 1
    If u wanna know it that exactly, its an URI which will lead to a track and play it. As I said I just don't think that this is the problem, I think its some kind of mistake that I made or line that I forgot and I just found something, im going to update right away – Loominarteyy Jul 30 '15 at 10:56
  • 1
    I updatet my post ↑ I think this is what you were askin for? – Loominarteyy Jul 30 '15 at 11:56
  • 1
    The target SDK Version is 19. The `.setExact()` didn't change anything. I'm wondering why my `onReceive`-Method is not getting fired – Loominarteyy Jul 30 '15 at 12:29

3 Answers3

1

It seems like your wakeLock is part of an activity, but your activity goes to pause mode (at least) when screen is locked.

You need to use a Service to make your radio/music playing, and then wake-locking it to keep the device awake to continue to play.

galex
  • 3,279
  • 2
  • 34
  • 46
0

I'm working on an App which is supposed to open a link to some music which will be played automatically through an app.

It sounds like you are getting the WakeLock correctly.

However you are trying to open music in a different app, which does not have a wakelock.

Your wakelock is only for your app, not for the other app. You have no way to control that other app.


Note that there are other (possibly better) approaches to accomplish what you want:

To Keep the screen on with an Activity:

  • consider using the FLAG_KEEP_SCREEN_ON flag
  • this is the recommended approach if your app is in the foreground

To Wake the device up from sleep with a BroadcastReceiver:


Alarm type apps also typically need to display their activities over the lock screen. i.e. when the phone is locked, you may want to show your activity to the user anyway.

This seems out of scope of this question, but there is lots of info at this related post:

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
  • Sounds understandable but why is it working when the screen is locked but not sleeping then? I think I just need a way to wake up the device and it should work – Loominarteyy Jul 29 '15 at 15:59
  • If "the screen is locked but not sleeping", then as you said, it is *not sleeping*. So no need for a `WakeLock` – Richard Le Mesurier Jul 29 '15 at 16:03
  • Yeah but how do I get my phone awake with the alarm? I thought I would need a wakelock for that. – Loominarteyy Jul 29 '15 at 16:11
  • @Loominarteyy It sounds like you are trying to make an alarm clock type app. Are you launching your own activity, or service? I have updated answer with some more info – Richard Le Mesurier Jul 30 '15 at 07:05
  • 1
    As I said, I already tried it with FLAG_KEEP_SCREEN_ON and unfortunately, it doesn't work, u know why? Right now I'm trying it with a WakefulBroadcastReceiver and a service like galex suggested. But for some reason, my receiver won't start, I'm going to update my post up there asap with my code – Loominarteyy Jul 30 '15 at 09:34
  • For some reason, the FLAG_KEEP_SCREEN_ON just does nothin :c I tried it again because I thought that maybe I made a mistake yesterday but still – Loominarteyy Jul 30 '15 at 10:17
  • I just dont got it, why is my `MyWakefulReceiver` not receiving anything? Did I miss something? – Loominarteyy Jul 30 '15 at 12:03
0

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

Loominarteyy
  • 31
  • 1
  • 8