---
title: "Android开机自动启动程序设置"
author: "monner"
date: "2010-08-08T22:59:19Z"
tags: []
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2010-08-08T22:59:19Z"
id: 732291
url: "https://monner.iteye.com/blog/732291"
---

### [Android开机自启动](/blog/375515)

 

1.定义一个BroadcastReceiver

Java代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

01. **public** **class** BootReceiver **extends** BroadcastReceiver {
02. **public** **void** onReceive(Context ctx, Intent intent) {
03. Log.d("BootReceiver", "system boot completed");
04. //start activity
05. String action="android.intent.action.MAIN";
06. String category="android.intent.category.LAUNCHER";
07. Intent myi=**new** Intent(ctx,CustomDialog.**class**);
08. myi.setAction(action);
09. myi.addCategory(category);
10. myi.addFlags(Intent.FLAG\_ACTIVITY\_NEW\_TASK);
11. ctx.startActivity(myi);
12. //start service
13. Intent s=**new** Intent(ctx,MyService.**class**);
14. ctx.startService(s);
15. }
16. }

```
public class BootReceiver extends BroadcastReceiver {
	public void onReceive(Context ctx, Intent intent) {
		Log.d("BootReceiver", "system boot completed");
		//start activity
		String action="android.intent.action.MAIN";
		String category="android.intent.category.LAUNCHER";
		Intent myi=new Intent(ctx,CustomDialog.class);
		myi.setAction(action);
		myi.addCategory(category);
		myi.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		ctx.startActivity(myi);
		//start service
		Intent s=new Intent(ctx,MyService.class);
		ctx.startService(s);
	}
}
```

 2.配置Receiver的许可,允许接收系统启动消息，在AndroidManifest.xml中：

Xml代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

1. **&lt;uses-permission** android:name="android.permission.RECEIVE\_BOOT\_COMPLETED"**/&gt;**

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

 3.配置Receiver,可以接收系统启动消息，在AndroidManifest.xml中

Xml代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

1. **&lt;receiver** android:name=".app.BootReceiver"**&gt;**
2. **&lt;intent-filter&gt;**
3. **&lt;action** android:name="android.intent.action.BOOT\_COMPLETED"**/&gt;**
4. **&lt;category** android:name="android.intent.category.HOME" **/&gt;**
5. **&lt;/intent-filter&gt;**
6. **&lt;/receiver&gt;**

```
        <receiver android:name=".app.BootReceiver">
        	<intent-filter>
        		<action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.HOME" />
        	</intent-filter>
        </receiver>
```

 4.启动模拟器，可以看到系统启动后，弹出一个对话框。