`
iame
  • 浏览: 130217 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Android开机自启动

阅读更多

Android SDK 1.1验证可行

 

1.定义一个BroadcastReceiver

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中:

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

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

        <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.启动模拟器,可以看到系统启动后,弹出一个对话框。

 

分享到:
评论
4 楼 hzjsftw 2014-03-14  
LZ,请教个问题,我的软件也是自启动的,现在的问题是,开机自启动的时候出现“抱歉,应用已停止”,在模拟器上是好的,然后在真机上调试也是好的,就是打包以后,正式发布,出现这种问题,而且是间歇性的,有时候出现,有时候可以正常启动
3 楼 www_JE 2011-11-01  
zjc198805 写道
LZ,在2.3真机中无法监听到系统的BOOT_COMPLETED广播,而无法进入自己的OnReceiver方法该怎么办呢?

应该是因为应用在sd卡中,广播在sd卡加载之前就发出所以接收不到
在manifest文件中设定应用安装到手机内存而不是外部存储中应该能解决问题:
android:installLocation="internalOnly"
希望对你有帮助
2 楼 zjc198805 2011-10-27  
LZ,在2.3真机中无法监听到系统的BOOT_COMPLETED广播,而无法进入自己的OnReceiver方法该怎么办呢?
1 楼 clzqwdy 2009-11-24  
有没有完整的源代码的?

谢谢!clzqwdy (at) 163.com

相关推荐

Global site tag (gtag.js) - Google Analytics