思路:

第一种方案:屏蔽除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))

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐