Chen Kinnrot
Chen Kinnrot

Reputation: 21015

Keep activity on when screen is off

I got a service that start a new activity, while screen is off. I want the activity to stay on paused mode, and not get to the stopped mode. Is there a way to achieve this?

Upvotes: 13

Views: 4196

Answers (7)

LoveForDroid
LoveForDroid

Reputation: 1142

Lame hack would be to call onPause() method inside onStop() of activity

Upvotes: 0

Mahdi-Malv
Mahdi-Malv

Reputation: 19190

You can't do that. it will be killed anyway. but you can use a Service to receive Broadcast of Intent.ACTION_SCREEN_ON and relaunch activity from Service. to restart your Activity from service check here

Upvotes: 1

Jorge Cevallos
Jorge Cevallos

Reputation: 3678

Why do you need to do this? Maybe best solution is to run it as a service as suggested by @DeeV. However, depending of what you need to do, this could help:

Ask Android to notify you when screen is turned on (and off if required). So your activity can be resumed and started immediately and you can perform any action.

To do that:

  • Create a class which extends Application
  • Register it in your Manifest:
  • In public void onCreate() {..., add this:

    IntentFilter ioff = new IntentFilter(Intent.ACTION_SCREEN_OFF);

    IntentFilter ion = new IntentFilter(Intent.ACTION_SCREEN_ON);

        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
    
                if (Intent.ACTION_SCREEN_OFF.equals(action)) {
                    //TODO
                } else {
                    //TODO
                }
            }
        };
    
        context.registerReceiver(broadcastReceiver, ioff);
        context.registerReceiver(broadcastReceiver, ion);
    
    • in public void onTerminate() {..., add this:

if (broadcastReceiver != null) { context.unregisterReceiver(broadcastReceiver); }

Upvotes: 0

JackTools.Net
JackTools.Net

Reputation: 734

I do not exactly now what you mean with paused mode or stopped mode. If you mean that the cpu keep wake up take a look at this.

With partial_wake_look you can have a long running AsyncTask for example also when the screen is of. Dont't forget to release the wakelook.

Upvotes: 0

jnthnjns
jnthnjns

Reputation: 8925

You can't override the onDestroy() method, and each OS version handles how an application is "killed" differently.

The Android developer documentation makes reference to an application being in a “killable” state. While Android tries to keep the process of an application resident even after it has exited (i.e. after onDestroy), it does need to be able to kill these processes in low-resource situations to reclaim memory. The states in which an application is killable differ per OS version. On all versions of Android, applications that have returned from onStop or onDestroy are silently killable. On versions of Android prior to Honeycomb, applications that had returned from onPause were also killable. Being killable simple means that Android reserves the right to terminate your application’s process at any time without running even another instruction of your app’s code. In other words, if you have any state that must be recoverable (such as a player’s game progress, items, awards, etc) you must save those to persistent storage no later than the last callback before entering a killable state.

In addition, while applications can run native threads even when they are in a killable state and even post-onDestroy, this is to be avoided, since the process kill will also kill those threads. This could cause all manner of corruption and shutdown issues.

Source

Upvotes: 1

SeanPONeil
SeanPONeil

Reputation: 3910

There is no way to achieve this. Your app is tied to the Activity lifecycle, and has to break down/restore the Activities to work with the lifecycle.

Upvotes: 0

DeeV
DeeV

Reputation: 36035

No. You don't have control over the Activity lifecycle. Any processes that you need to run while the screen is off must be executed in a Service. You must rebuild the application state whenever the screen is turned back on. Always assume the Activity can be wiped from memory at any time.

Upvotes: 7

Related Questions