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