How to wake a phone remotely?
Asked Answered
R

2

6

I want to build an Android app that needs to be launched remotely over 3G (after getting a push notification over a socket).

I did some research and it seems it becomes very complicated as soon as the screen turns off and also because of Android killing idle sockets.

Is there an example project that demonstrates how to implement this reliably? I found the WakefulIntentService library, but it doesnt take in account that the socket needs to be kept alive.

An alternative would be to poll a certain URL periodically for a wakeup signal, but that would introduce a large delay before the device detects it needs to launch the app, depending on the polling interval.

Risotto answered 18/2, 2014 at 22:22 Comment(1)
hi @Muis, have you figure out how to make it? I want to make this too, and I found some phones slept and cannot receive Firebase Push Notification immediately, do you match same problem?Weekender
A
2

Have you looked into GCM or parse.com to send and receive the push?

I do not think those tend to be killed that easily.

That being said, if you have not already, you should have your socket running in a Service. Then it can run in the background independent of an activity being alive and it can be started when the device boots. Furthermore that would decrease the likelihood of Android shutting it down.

Andorra answered 18/2, 2014 at 22:40 Comment(6)
Thanks for pointing me to GCM, it seems exactly what I was looking for, and incoming GCM messages should arrive even when the screen is off according to the docs. Looks perfect!Risotto
Glad to be able to help. I am personally happy with parse.com for its simplicity and they have servers running for you. Using GCM you have to implement your own server. Also, parse.com recently announced that they support GCM too (as an alternative to theire own notification push channel) but have not looked into that yet.Andorra
@Muis: services are not full proof. Don't know about those libraries but you should maybe consider an AlarmManager with a small interval to wake a Receiver which delegates to wakeful intent serviceCheyney
@Cheyney but IntentServices are only singleshot, this question is about keeping a socket open and running. I do not believe your suggestion applies that well to that sort of background tasks.Andorra
He considered polling so I am suggesting a (the) polling technique in androidCheyney
@Cheyney Seems you are right. Misunderstood it to be a continuously open connection (not that it would be a good idea).Andorra
B
0
public abstract class WakeLocker {
    private static PowerManager.WakeLock wakeLock;

    public static void acquire(Context context) {
        if (wakeLock != null) wakeLock.release();

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, "WakeLock");
        wakeLock.acquire();
    }

    public static void release() {
        if (wakeLock != null) wakeLock.release(); wakeLock = null;
    }
}

In the manifest add this line:

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

When you want to wake up use:

WakeLocker.acquire(this);

after you finish call

WakeLocker.release();
Bunn answered 18/2, 2014 at 22:41 Comment(2)
Thanks for your code, but its kind of a chicken-egg problem: I need to call .aquire() in response to a TCP packet, to wake the phone and launch a certain app. But I will never receive the packet as long as the phone is in standby?Risotto
I think I should be able to call .aquire() in response to a GCM message, which would solve the issue in the comment above.Risotto

© 2022 - 2024 — McMap. All rights reserved.