Android Analog freeze Screen code implementation

Source: Internet
Author: User

Ideas:


The first scenario: shielding other keys except power, shielding the status bar dropdown, shielding the touch screen.
The code is as follows:


System Signature Required:
Add Permissions:
<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); Prohibit Home
Disablestatusbar ();//Prohibit status bar
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 () {
Whether the first sentence can be proposed to

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 ();
}


}




This way, you can simulate all operations except power.


2:
Above behavior, can not block power, the following scheme to achieve shielding power
The AM command operation is used here.


Specific operation: Enter the cmd input adb shell into the terminal


Am Hang can.


3: Use APK to implement the AM Command scheme:
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;
}


You need to add permissions:
<uses-permission android:name= "Android.permission.SET_ACTIVITY_WATCHER"/>
The specific code can see the Hang function inside the Activitymanagerserver.java.


4: Full APK source code is:
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= "/>"


<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 () {
Whether the first sentence can be proposed to

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 is:
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))



Android Analog freeze Screen code implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.