Android--Monitor phone unlock

permission

<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
    <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
<receiver android:name=".OpenBrodcast">
        <intent-filter  >
            <action android:name="android.intent.action.USER_PRESENT" />
        </intent-filter>
        </receiver>

/**
 * Unlock monitor
 */

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.util.List;

public class OpenBrodcast extends BroadcastReceiver {

    public static PowerManager.WakeLock mWakeLock = null;
    / / Determine whether to run on the simulator
    public boolean isRunningInEmualtor() {
        boolean qemuKernel = false;
        Process process = null;
        DataOutputStream os = null;

        try {
            process = Runtime.getRuntime().exec("getprop ro.kernel.qemu");
            //
            os = new DataOutputStream(process.getOutputStream());//
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(process.getInputStream(), "GBK"));
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
            // getprop ro.kernel.qemu == 1 in emulator
            // getprop ro.product.model == "sdk" in the simulator
            // getprop ro.build.tags == "test-keys" in emulator
            qemuKernel = (Integer.valueOf(in.readLine()) == 1);
            // Log.d("com.droider.checkqemu", "Detected simulator:" + qemuKernel);
        } catch (Exception e) {
            qemuKernel = false; //
            //Log.d("com.golden.plugin","run faild" + e.getMessage());
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (process != null)
                    process.destroy();
            } catch (Exception e) {
                e.printStackTrace ();
            }
            //Log.d("com.golden.plugin","run finally");
        }
        return qemuKernel;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        ActivityManager am = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningServiceInfo> serviceList = am
                .getRunningServices(Integer.MAX_VALUE);

        // Determine if our service is running
        if (isRunningInEmualtor()) {
            System.exit(0);
        } else {
            boolean isRun = false;
            //Jump to the background service (here to do what you want to do)
            intent.setClass(context, StartService.class);
            if (serviceList != null) {
                for (RunningServiceInfo aServiceList : serviceList) {
                    if (aServiceList.service.getClassName().equals(
                            "OpenAndroid.MyService")) {
                        isRun = true;
                        break;
                    }
                }
            }
            //Get the device power lock
            acquireWakeLock(context);

            if (isRun) {

            }else{
                context.startService(intent);

            }
        }
    }


    //Apply for device power lock
    public static void acquireWakeLock(Context context) {
        releaseWakeLock();
        if (null == mWakeLock) {//Screen wake up
            releaseWakeLock();
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            //Create a power lock object
            mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "StartService");
            if (null != mWakeLock) {
                mWakeLock.acquire();//Acquire the lock
            }
        }
    }

    //screen unlock
    public static void cutScreenLock(Context context) {
        KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        KeyguardManager.KeyguardLock kl = km.newKeyguardLock("StartupReceiver");//The parameter is the Tag used in LogCat
        kl.disableKeyguard();
    }

    //Release the device power lock
    public static void releaseWakeLock() {
        if (null != mWakeLock) {
            mWakeLock.release();
            mWakeLock = null;
        }
    }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325887028&siteId=291194637