Several implementations of the Android system shutdown or restart

Source: Internet
Author: User

before the work encountered some of the Android system shutdown or restart of the system changes, so, did some attempts, also collected a bit of information, now tidy up, do some summary, to facilitate learning or future work needs.

The default SDK does not provide the app developer direct Android system shutdown or restart API interface, generally speaking, the implementation of the Android system shutdown or restart, requires higher permissions (System privileges and even root privileges). So, in the General app, if you want to implement shutdown or restart, either declare the system permissions in the app, or indirectly implement the system shutdown or restart through some kind of "indirect" way, such as broadcast or reflection. In addition, is put in the source environment to compile, this has the advantage that can directly call the Android non-public API, this is the effect that ECLIPSE+SDK can't achieve. Here are some of the ways I try it myself:

The first way: the way of broadcasting

Broadcast is one of the four basic components of Android, which we often call radio. The Android system itself contains many broadcasts, constantly listening to every broadcast registered in the system and ready to respond. Among them, there are broadcasts about shutting down or restarting: Intent.action_request_shutdown and Intent.action_reboot, by sending these two broadcasts, Android can automatically receive broadcasts and respond to shutdown or restart operations. The implementation method is as follows

Broadcast mode power off restart case R.ID.SHUTDOWN_BTN1:LOG.V (TAG, "Broadcast->shutdown");                Intent Intent = new Intent (intent.action_request_shutdown);//action_request_shutdown                Intent.putextra ( Intent.extra_key_confirm, false);//Where false is changed to true, the confirmation window Intent.setflags if the shutdown is turned on                (intent.flag_activity_new_task );                StartActivity (intent); Break;case r.id.reboot_btn1:log.v (TAG, "broadcast->reboot");                Intent Intent2 = new Intent (intent.action_reboot);                Intent2.putextra ("nowait", 1);                Intent2.putextra ("Interval", 1);                Intent2.putextra ("window", 0);                Sendbroadcast (Intent2);  Break





Several implementations of the Android system shutdown or restart

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.