思路:
第一種方案:屏蔽除power外的其他按鍵,屏蔽狀態欄下拉,屏蔽觸屏。
代碼如下:
需要系統簽名:
添加權限:
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;
}
需要添加權限:
具體代碼可以查看activitymanagerserver.java 裡面的hang函數。
4:完整apk源碼為:
AndroidManifest.xml
package="com.example.demo"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="18" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/hello_world"
android:screenOrientation="portrait"
android:theme="@style/AppTheme" >
android:name="com.example.demo.MainActivity"
android:label="@string/app_name" >
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))