Started searching the internet for a lot of programs about shutting down the computer. On-line mainstream shutdown program is in Java into send request shutdown intent., as follows:
Intent Intent = new Intent (intent.action_request_shutdown); Intent.putextra (intent.extra_key_confirm, false); Intent.setflags (intent.flag_activity_new_task); Mcontext.startactivity (Intent);
You need to add the relevant permissions:
android:shareduserid= "Android.uid.system"
<uses-permission android:name= "Android.permission.SHUTDOWN"/
There is a problem here: To add System program permissions to the program. How do you add system permissions to your program?
My solution is to write a system shutdown program, build the SHUTDOWN.APK application, and then the project code will be able to call this shutdown.apk to shut down the machine perfectly.
1. To get the shutdown.apk application. However, this application needs to be upgraded to the system app level. Getting the system level is best compiled under the source code to build the APK,
This is easy to improve the system app level.
1.1, create the shutdown project. Mainly related to Shutdownactivity.java and androidmainfest.xml.
1.2, Shutdownactivity.java source code as follows:
public class Shutdownactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Intent it = new Intent (" Android.intent.action.ACTION_REQUEST_SHUTDOWN "); It.putextra (" Android.intent.extra.KEY_CONFIRM ", false); It.setflags (Intent.flag_activity_new_task); startactivity (it);}}
1.3, Androidmainfest.xml source code as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.hutao.shutdown "android:versioncode=" 1 "android:shareduserid= "Android.uid.system"
Android:versionname= "1.0" > <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/>"<uses-permission android:name= "Android.permission.SHUTDOWN"/><application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@strin G/app_name "Android:theme=" @style/apptheme "> <activity android:name="com.hutao.shutdown.ShutDownActivity"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>
1.4, will be built to copy the project to the/packages/apps/directory, and then in the current source top-level source build/envsetup.sh. The top level of my source code is
Rowboat-android below. Then delete the drawable-xxhdpi, VALUES-SW600DP, Value-sw720dp-land below Res. In
/rowboat-android/packages/apps/shutdown/Create android.mk file below
Local_path:= $ (call My-dir) include $ (clear_vars) Local_module_tags: = Optionallocal_src_files: = $ (call All-java-files-under, src) local_package_name: = shutdownlocal_certificate: = Platforminclude $ (BUILD_PACKAGE) # Use the Folloing include to make we test Apk.include $ (call all-makefiles-under,$ (Local_path))
Then the MM command is compiled under the current directory and generated in out/target/product/system/app/shutdown.apk
2. Call shutdown.apk to shut down the machine.
2.1 Calling the SHUTDOWN.APK code
Intent myintent = new Intent (); Myintent.addflags (intent.flag_activity_new_task); Myintent.setclassname ("Com.hutao.shutdown", "com.hutao.shutdown.ShutDownActivity"); StartActivity (myintent);
2.2 There are two forms of invoking the shutdown interface:
That is, It.putextra ("Android.intent.extra.KEY_CONFIRM", false),//false does not display a dialog box confirming shutdown, directly shuts down the computer.
or It.putextra ("Android.intent.extra.KEY_CONFIRM", true);//true displays a dialog confirming the shutdown, letting the user choose whether to confirm the shutdown
Android 2.3.4 code for shutdown