Android shuts down automatically after 5 Seconds of disconnecting power

Source: Internet
Author: User

Recently in doing the car recorder thing, because the lithium battery capacity on the vehicle device is smaller, so in the parking stall, save the data after the automatic shutdown, because the shutdown permissions are not open to ordinary applications, so need to compile code under the source. Can write their own broadcastreceiver put into the setting source code, can also be a standalone application.

Manifest nodes need to declare system-level UID:

android:sharedUserId="android.uid.system"

and Shutdown permissions:

<uses-permission android:name="android.permission.SHUTDOWN"/>

These two points are in setting's androidmanifest, so we don't have to add them manually.
And then I declared two global variables in application to store the current power state and the number of seconds the power has been disconnected:

    /**     * 电源是否连接     */    publicstaticbooleantrue;    /**     * 电源断开的秒数     */    publicstaticint0;

Then there is powerstatechangereceiver:

 PackageCom.android.settings;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;ImportAndroid.content.Intent;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.widget.Toast; Public  class powerstatechangereceiver extends broadcastreceiver {    PrivateContext context;@Override     Public void OnReceive(context context, Intent Intent) { This. Context = Context;if("Android.intent.action.ACTION_POWER_CONNECTED". Equals (Intent. Getaction ())) {Screenshot.ispowerconnect =true;//toast.maketext (Context, "CONNECTED", Toast.length_short). Show ();}Else if("Android.intent.action.ACTION_POWER_DISCONNECTED". Equals (Intent.getaction ())) {Screenshot.ispowerconnect =false;//toast.maketext (context, "disconnected", Toast.length_short). Show ();            NewThread (NewShutdownthread ()). Start (); }    }/** * Shutdown * * @param Context * *     Public void shutdown(Context context) {Try{Intent Intent =NewIntent ("Android.intent.action.ACTION_REQUEST_SHUTDOWN"); Intent.putextra ("Android.intent.extra.KEY_CONFIRM",false);            Intent.setflags (Intent.flag_activity_new_task);        Context.startactivity (Intent); }Catch(Exception e) {} toast.maketext (context, context.getresources (). getString (R.string.shutdown_now), toast.length_short). Sho    W (); } Public  class shutdownthread implements Runnable {        @Override         Public void Run() { while(true) {Try{Thread.Sleep ( +); Message message =NewMessage (); Message.what =1; Shutdownhandler.sendmessage (message);if(!                    Screenshot.ispowerconnect) {screenshot.offsecond++; }Else{Screenshot.offsecond =0; }                }Catch(Interruptedexception e)                {E.printstacktrace (); }            }        }    }FinalHandler Shutdownhandler =NewHandler () { Public void Handlemessage(Message msg) {Switch(msg.what) { Case 1:if(Screenshot.offsecond = =5&&!                Screenshot.ispowerconnect) {shutdown (context); } Break;default: Break; }        }    };}

In addition is the statement of Androidmanifest.xml to Reveiver in setting:

<receiver  android:name  = ". Powerstatechangereceiver ";  <intent-filter< /span>>  <action  android:name  =" Android.intent.action.ACTION_POWER_CONNECTED "/>  <action  andr            Oid:name  = "Android.intent.action.ACTION_POWER_DISCONNECTED" />         </intent-filter ;  </receiver ;   

Okay, it's done.

Copyright Notice: This article original, reproduced please indicate the source: Http://blog.csdn.net/zhoumushui

Android shuts down automatically after 5 Seconds of disconnecting power

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.