android 模拟冻屏 代码实现

思路:


第一种方案:屏蔽除power外的其他按键,屏蔽状态栏下拉,屏蔽触屏。
代码如下:


需要系统签名:
添加权限:
    <uses-permission android:name="android.permission.STATUS_BAR" />
    <uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />


package com.example.demo;


import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.app.StatusBarManager;
import android.view.MotionEvent;
public class MainActivity extends Activity {
public static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000;
private StatusBarManager mStatusBarManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(FLAG_HOMEKEY_DISPATCHED); //禁止home
disableStatusbar();// 禁止状态栏
setContentView(R.layout.activity_main);


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return true;
}
    private void disableStatusbar(){
        // 第一句是否可以提出去
   
        mStatusBarManager = (StatusBarManager) this.getSystemService(this.STATUS_BAR_SERVICE);
        mStatusBarManager.disable(StatusBarManager.DISABLE_EXPAND);
    }


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_HOME:
return true;


default:
break;
}
return super.onKeyDown(keyCode, event);
}


@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_HOME:
return true;


default:
break;
}
return super.onKeyLongPress(keyCode, event);
}


@Override
public void onBackPressed() {
// TODO Auto-generated method stub
// super.onBackPressed();
}


}




如此下来,可以模拟除了power外的所有操作。


2:
以上行为,没法屏蔽power,下面方案实现屏蔽power
这里使用了am命令操作.


具体操作:进入cmd 输入adb shell进入终端


am hang 即可。


3:使用apk实现am命令方案:
runCommand("am hang"); 
private boolean runCommand(String command) {
java.lang.Process process = null;
try {
process = Runtime.getRuntime().exec(command);
Log.i("lxm", "Runtime");
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
Log.i("lxm", "error");
return false;
} finally {
try {
process.destroy();
} catch (Exception e) {


}
}
Log.i("lxm", "finally");
return true;
}


需要添加权限:
  <uses-permission android:name="android.permission.SET_ACTIVITY_WATCHER" />
具体代码可以查看activitymanagerserver.java 里面的hang函数。


4:完整apk源码为:
AndroidManifest.xml




<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.demo"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />


    <uses-permission android:name="android.permission.STATUS_BAR" />
    <uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
    <uses-permission android:name="android.permission.SET_ACTIVITY_WATCHER" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/hello_world"
android:screenOrientation="portrait"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.demo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>


MainActivity.java


package com.example.demo;


import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.app.StatusBarManager;
import android.view.MotionEvent;
public class MainActivity extends Activity {
public static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000;
private StatusBarManager mStatusBarManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(FLAG_HOMEKEY_DISPATCHED);
disableStatusbar();
setContentView(R.layout.activity_main);
java.lang.Process process = null;   
        runCommand("am hang"); 
}
private boolean runCommand(String command) {
java.lang.Process process = null;
try {
process = Runtime.getRuntime().exec(command);
Log.i("lxm", "Runtime");
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
Log.i("lxm", "error");
return false;
} finally {
try {
process.destroy();
} catch (Exception e) {


}
}
Log.i("lxm", "finally");
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return true;
}
    private void disableStatusbar(){
        // 第一句是否可以提出去
   
        mStatusBarManager = (StatusBarManager) this.getSystemService(this.STATUS_BAR_SERVICE);
        mStatusBarManager.disable(StatusBarManager.DISABLE_EXPAND);
    }


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_HOME:
return true;


default:
break;
}
return super.onKeyDown(keyCode, event);
}


@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_HOME:
return true;


default:
break;
}
return super.onKeyLongPress(keyCode, event);
}


@Override
public void onBackPressed() {
// TODO Auto-generated method stub
// super.onBackPressed();
}


}


android.mk 为:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)


LOCAL_MODULE_TAGS := optional


LOCAL_SRC_FILES := $(call all-java-files-under, src)


#LOCAL_STATIC_JAVA_LIBRARIES := com.android.phone.common


LOCAL_PACKAGE_NAME := Frozen
LOCAL_CERTIFICATE := platform


#LOCAL_PROGUARD_FLAG_FILES := proguard.flags


include $(BUILD_PACKAGE)


# Use the folloing include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。