首先定义一个什么事都不做的Activity,作为应用的启动Activity.这个Activity一定要定义的,要不然接收不到Broadcast的。
再来就是定义一个Broadcast类,可以收到android.intent.action.BOOT_COMPLETED广播的。这个类在收到android.intent.action.BOOT_COMPLETED广播后,5s后关机。
代码如下:
- static final String ACTION_COMP = "android.intent.action.BOOT_COMPLETED";
- static final String TAG = "AlarmShutdownReceiver";
- private Timer mTimer = null;
- private int mfristtime = 0;
- @Override
- public void onReceive(Context context, Intent intent) {
- final Context icontext = context;
- Log.v(TAG,"onReceive+");
- if(intent.getAction().equals(ACTION_COMP))
- {
- Log.v(TAG,"boot completed+");
- Toast.makeText(context, "shutdown service start, waitting 5s to shutdown", Toast.LENGTH_SHORT).show();
- mTimer = new Timer();
- mTimer.schedule(new TimerTask() {
- @Override
- public void run() {
- mfristtime++;
- if(1 == mfristtime)
- {
- Log.v(TAG,"send shutdown intent +");
- Intent shutdown = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
- shutdown.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
- shutdown.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- icontext.startActivity(shutdown);
- Log.v(TAG,"send shutdown intent +");
- }
- }
- }, 5*1000, 5*1000);
- Log.v(TAG,"boot completed-");
- }
- Log.v(TAG,"onReceive-");
- }
- }
public class AlarmShutdownReceiver extends BroadcastReceiver {
static final String ACTION_COMP = "android.intent.action.BOOT_COMPLETED";
static final String TAG = "AlarmShutdownReceiver";
private Timer mTimer = null;
private int mfristtime = 0;
@Override
public void onReceive(Context context, Intent intent) {
final Context icontext = context;
Log.v(TAG,"onReceive+");
if(intent.getAction().equals(ACTION_COMP))
{
Log.v(TAG,"boot completed+");
Toast.makeText(context, "shutdown service start, waitting 5s to shutdown", Toast.LENGTH_SHORT).show();
mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
mfristtime++;
if(1 == mfristtime)
{
Log.v(TAG,"send shutdown intent +");
Intent shutdown = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
shutdown.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
shutdown.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
icontext.startActivity(shutdown);
Log.v(TAG,"send shutdown intent +");
}
}
}, 5*1000, 5*1000);
Log.v(TAG,"boot completed-");
}
Log.v(TAG,"onReceive-");
}
}
Intent.ACTION_REQUEST_SHUTDOWN;
Intent.EXTRA_KEY_CONFIRM, false;
这两个属性应该是系统级别的属性,对外是不公开的,在android 源码下是可以编译通过的。
AndroidManifest.xml定义如下:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="XXXX"
- android:versionCode="1"
- android:versionName="1.0"
- android:sharedUserId="android.uid.system">
- <uses-sdk android:minSdkVersion="15" />
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
- <uses-permission android:name="android.permission.SHUTDOWN" />
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <receiver android:name=".AlarmShutdownReceiver"
- android:enabled="true"
- android:exported="true"
- >
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED"/>
- </intent-filter>
- </receiver>
- <activity
- android:name=".AutoShutdown"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="XXXX"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="android.uid.system">
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.SHUTDOWN" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver android:name=".AlarmShutdownReceiver"
android:enabled="true"
android:exported="true"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<activity
android:name=".AutoShutdown"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
另外Android.mk文件定义要包含以下语句:
- LOCAL_CERTIFICATE := platform
LOCAL_CERTIFICATE := platform
mm 编译就可以了。