سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top