Android automatically shuts down after 5 seconds of Power disconnection
Recently, I am working on a car recorder. Because the lithium battery capacity on the on-board equipment is relatively small, I need to shut down automatically after I stop the device and save the data. Because the Shutdown permission is not open to common applications, therefore, you need to compile the code in the source code. You can write a BroadcastReceiver into the Setting source code or an independent application.
The manifest node must declare the system level uid:
android:sharedUserId=android.uid.system
And shutdown permission:
These two points are available in the AndroidManifest of Setting, so we do not need to manually add them.
Then I declare two global variables in the Application to store the current power status and the number of seconds that the power supply has been disconnected:
/*** Whether the power supply is connected */public static boolean isPowerConnect = true;/*** the number of seconds when the power supply is disconnected */public static int OffSecond = 0;
Then there is PowerStateChangeReceiver:
package com.android.settings;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Handler;import android.os.Message;import android.widget.Toast;public class PowerStateChangeReceiver extends BroadcastReceiver { private Context 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(); new Thread(new shutdownThread()).start(); } } /** * Shutdown * * @param context */ public void shutdown(Context context) { try { Intent intent = new Intent( 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).show(); } public class shutdownThread implements Runnable { @Override public void run() { while (true) { try { Thread.sleep(1000); Message message = new Message(); message.what = 1; shutdownHandler.sendMessage(message); if (!Screenshot.isPowerConnect) { Screenshot.OffSecond++; } else { Screenshot.OffSecond = 0; } } catch (InterruptedException e) { e.printStackTrace(); } } } } final Handler shutdownHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 1: if (Screenshot.OffSecond == 5 && !Screenshot.isPowerConnect) { shutdown(context); } break; default: break; } } };}
In addition, the reveiver declaration in setting's AndroidManifest. xml is as follows: