0

I need to force the android device to stay alive while the application is running. There any way to do this ? I read here : Is there a way to force an android device to stay awake? about this, I tried to do this but probably I don't know to use correctly a Service.

This is the code I use :

public class WakeLockService extends Service {

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public void onCreate() {
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
    wl.acquire();
}
@Override
public void onDestroy() {
    wl.release();
}

and in the first Activity of my Application, I put this :

Intent s = new Intent(this, WakeLockService.class);
startService(s);

Is it correct what I'm doing? Anyone can help me to do this ? Thanks in advance.

Community
  • 1
  • 1
Gabrielle
  • 4,933
  • 13
  • 62
  • 122
  • Please define "the application is running". Do you mean when a specific activity is in the foreground? Why did you create a service just to have a `WakeLock`? – CommonsWare Nov 02 '12 at 14:59
  • This is what I found on SO. I don't know another way to make the android device to stay alive. Can you help me ? – Gabrielle Nov 02 '12 at 15:02
  • **Please define "the application is running"**. Unless and until you can clearly explain what you mean by "the application is running", we cannot give you any reliable advice. – CommonsWare Nov 02 '12 at 15:04
  • In my application I have, for example, a syncronization server->mobile and this syncronization can run more then 5 minutes. I want to force the device to not enter into stand-by, to see when the syncronization process is finished. I want to say that when the application is open on device, the device to stay alive and to not enter in stand-by mode. – Gabrielle Nov 02 '12 at 15:09

2 Answers2

4

If you want the device to stay awake while it displays an activity of your app, you have to set the flag FLAG_KEEP_SCREEN_ON when creating the activity:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); // Unlock the device if locked
    window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); // Turn screen on if off
    window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // Keep screen on
    .....
}

Add the permission WAKE_LOCK in the manifest:

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

EDIT After seeing your last comment: yes, you need a service: note that the device will go to sleep anyway, but your service can continue to run if you make it clear to the user (you have to display a notification) and declare it STICKY:

public class yourservice extends Service
{
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        //The intent to launch when the user clicks the expanded notification
        /////////////////////////////////////////////////////////////////////
        Intent forPendingIntent = new Intent(this, si.test.app.activities.activity.class);
        forPendingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendIntent = PendingIntent.getActivity(this, 0, forPendingIntent, 0);

        Notification notification = new Notification(R.drawable.icon, "testapp", System.currentTimeMillis());
        notification.setLatestEventInfo(this, "testApp", "testApp is running", pendIntent);

        notification.flags |= Notification.FLAG_NO_CLEAR;
        startForeground (R.string.app_name, notification);
        return START_STICKY;
    }
    ...
}
Paolo Brandoli
  • 4,681
  • 26
  • 38
2

In my application I have, for example, a syncronization server->mobile and this syncronization can run more then 5 minutes. I want to force the device to not enter into stand-by, to see when the syncronization process is finished

The synchronization operation should be managed by some Android component, such as a service. That component can manage a WakeLock. Do not create some separate component purely for the WakeLock, as that other component has nothing whatsoever to do with your synchronization work.

For example, if your synchronization is being conducted via an IntentService, you could use my WakefulIntentService to keep the device awake while the work in onHandleIntent() is being conducted.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491