Android calls the shutdown and restart functions of the system, and android calls

Source: Internet
Author: User

Android calls the shutdown and restart functions of the system, and android calls

I compile package/apps/In the android source code, because the shutdown interface to be called is not open to the upper layer and cannot be called in eclipse.

I mainly introduce how to call the shutdown function of android, because some permissions and other conditions for shutdown are more than those for restart during debugging. Therefore, if the shutdown function can be implemented, then restart the instance.

Add the statement android: sharedUserId = "android. uid. system" to AndroidManifest. xml and upgrade the apk to system permission.

This statement is added after the version number.

Then write a system permission <uses-permission android: name = "android. permission. SHUTDOWN"/>

This permission is only available for system-level apk, so eclipse cannot compile.

Then, write two buttons in xml, one shutdown and one restart. xml is not introduced here.

To restart, you need to call android. intent. action. REBOOT. This interface is open to the upper layer and can be called directly:

Case R. id. reboot_btn:

Intent intent = new Intent (Intent. ACTION_REBOOT );

Intent. putExtra ("nowait", 1 );

Intent. putExtra ("interval", 1 );

Intent. putExtra ("window", 0 );

SendBroadcast (intent );

Break;

Finally, sendBroadcast can be sent only when it is a system-level apk.

To implement shutdown, you need to call android. intent. action. ACTION_REQUEST_SHUTDOWN. This interface is not open to the upper layer and cannot be called in eclipse, but it is available in the source code. Therefore, no error will be reported when compiling the apk in the source code.

Public static final String ACTION_REQUEST_SHUTDOWN = "android. intent. action. ACTION_REQUEST_SHUTDOWN"

Public static final String EXTRA_KEY_CONFIRM = "android. intent. extra. KEY_CONFIRM"

Intent I = new Intent (ACTION_REQUEST_SHUTDOWN );

I. putExtra (EXTRA_KEY_CONFIRM, false );

I. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK );

StartActivity (I );

I read a lot of blogs and wrote Intent I = new Intent (Intent. ACTION_REQUEST_SHUTDOWN );

I have tried it. Even if it is compiled in linux, an error is reported. Therefore, you need to define a constant and then use it directly. Do not use Intent.

EXTRA_KEY_CONFIRM, which must be defined by yourself, is not open to the upper layer.

Because it is compiled in linux, you need to write an Android. mk file. You can copy the file of another apk and make some modifications.

Copy code

LOCAL_PATH: = $ (call my-dir)

Include $ (CLEAR_VARS)

LOCAL_MODULE_TAGS: = optional

# Only compile source java files in this apk.

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

LOCAL_SDK_VERSION: = current

LOCAL_PACKAGE_NAME: = Reboot

LOCAL_CERTIFICATE: = platform

LOCAL_DEX_PREOPT: = false

Include $ (BUILD_PACKAGE)

# Use the following include to make our test apk.

Include $ (call all-makefiles-under, $ (LOCAL_PATH ))

Copy code

I am not very familiar with mk files. Let's talk about the key points:

LOCAL_PACKAGE_NAME: = project name

4.0 to compile the apk file, add LOCAL_DEX_PREOPT: = false to compile and generate the APK file. If the default value is true, the apk file cannot be installed and run independently.

Add LOCAL_CERTIFICATE: = platform to obtain system permissions.

I saw someone saying they want to delete the bin directory. The specific function is not cleared. I deleted it anyway.

Then in the root directory of the android source code. build/envsetup. sh

Lunch another version

Go to the directory where the project has mk files and execute mm

 

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.