0

I wrote this code to make my activity wake if screen is off.

private PowerManager mPM;
private PowerManager.WakeLock mPartialWakeLock;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

//some code

mPM = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
if (mPM == null) {
Log.e(TAG, "PowerManager is null");
}

try {
mPartialWakeLock = mPM.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK , "LOG");
    mPartialWakeLock.aquire();
}
catch (Exception e) {Log.i(TAG, "mPM.newWakeLock() EXCEPTION="+e.toString());}

The problem is when the screen is off, the application is paused.

ARM
  • 363
  • 1
  • 7
  • 18
  • You are checking for null and then use the mPM variable regardless...Also post more info - what is this ? A fragment - where it is used, what is the app doing etc. Post more (relevant) code – Mr_and_Mrs_D Jun 22 '13 at 14:07

3 Answers3

0

Try this one if you want to unlock screen while the activity called

Window wind;
wind = this.getWindow();
wind.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
wind.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
wind.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);

and if you prevent screen lock then just write the following code to your xml file will prevent the screen from locking

android:keepScreenOn="true"
dharmendra
  • 7,835
  • 5
  • 38
  • 71
0

I am currently using the following partial wake lock in an application just fine:

public class my_frag extends Fragment {
    WakeLock wl; 

    //on create, config changed, etc

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    setRetainInstance(true);        
    PowerManager pm = (PowerManager) this.getActivity().getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");  

    //I happen to have it in a button click event based on an async task 
    //(Side note: I should probably be using a Loader for my Async task but this works too) 
    //Just move it outside the button click if you don`t need it there

    connectButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (metrics_task != null)
            {
            Status s = metrics_task.getStatus();
            if (s.name().equals("RUNNING")){
                if (metrics_task.running){
                metrics_task.cancel(true);
                connectButton.setText("Start");
                metrics_task.running = false;
                wl.release(); <--releases it on async stop
                }
                else{
                    metrics_task = new start_metrics(ae);
                    metrics_task.execute();
                    wl.acquire(); <--starts it on async start
                }
            }
            else{

                metrics_task = new start_metrics(ae);
                metrics_task.execute();

            }
            }
            else{
                metrics_task = new start_metrics(ae);
                metrics_task.execute();
            }
        }
    });

Just remember to release it when you are not using it

TwinPrimesAreEz
  • 1,699
  • 1
  • 12
  • 16
0

Try to acquire the wake lock by extending Application class, this means that lock is acquired for the entire application: Code:

package com.ballytech.RemoteGamingClient.UserView;

import android.app.Application;
import android.content.Intent;
import android.os.PowerManager;
import android.provider.Settings;

/**
 * @author SDurai
 * 
 */
public class RCGApplication extends Application 
{
    private static final String TAG = RCGApplication.class.getSimpleName();
    private PowerManager.WakeLock mWakeLock = null;

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

        final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
        mWakeLock.acquire();
    }

    @Override
    public void onTerminate() {
        if (mWakeLock.isHeld())
            mWakeLock.release();
        super.onTerminate();
    }
}

Let me know if you have any other doubts. Ready to help!

Sathish
  • 137
  • 1
  • 4
  • **Don't forget to add the permission in Android Manifest :)** – Sathish Jun 26 '14 at 11:53
  • Your wakelook is never released as onTerminate won't be called on actual devices. This is only called on emulated devices (). See: http://developer.android.com/reference/android/app/Application.html#onTerminate%28%29 – Denis Loh Jan 28 '15 at 12:13
  • True. But we can add the same in onStop() right? Please pour in your thoughts. – Sathish Feb 12 '15 at 06:05
  • 1
    It is recommended that you use the pair of functions like onCreate/onDestroy or onStart/onStop. So, if you release the wake lock in onStop, you should aquire it in onStart. Otherwise it might happen, that the lock is released but never re-aquired later. – Denis Loh Feb 12 '15 at 08:15
  • If you are acquiring wake lock for Application start then you would want it for whole application life cycle, and ideally it should be released when application is being terminated. The truth is there is no callback for application termination on real device. So hypothetically you should avoid acquiring on Application.onCreate(). P.S. *onStop()* does not belongs to Application class, rather it is an API from Activity class. – A.B. Feb 02 '17 at 17:54