Android根据用户设置决定是否在开机时启动应用程序
2012-05-23 13:42
357 查看
今天要讨论的是:在Android手机上如何根据用户的设置来决定是否在开机时启动应用程序。这句话不知道怎么说才能通顺,就先详细说一下要解决的问题吧。
我们都知道要让应用程序在开机时启动,首先是在manifest中添加权限,注册receiver,给receiver添加action,然后在receiver的onReceive方法中启动一个service或者activity。
如果某个应用程序想让用户决定是不是在开机时启动,用户选择是,这个应用程序就接收bootcompleted广播,用户选择否,这个程序就不接收bootcompleted广播。可是由于receiver是注册在manifest文件中,怎样让它在手机启动时不响应bootcompleted广播了。
有人说在onReceive方法中检查SharedPreferences,这是不对的,即使它执行完onReceive后就结束了,但它毕竟是启动了。
解决的方法是使用PackageManager,然后调用它的setComponentEnabledSetting方法,这个方法可以让一个组件失效或者生效,如果让这个receiver失效了,他就接收不到广播了,当然让它生效了,就能接收到广播了。
先贴代码片段
首先是权限
<uses-permissionandroid:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
接着注册receiver
<receiverandroid:name=".MyReceiver">
<intent-filter>
<actionandroid:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
然后是onReceiver方法,我选择启动一个Activity测试
publicclassMyReceiverextendsBroadcastReceiver{
@Override
publicvoidonReceive(Contextcontext,Intentintent){
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Toast.makeText(context,"receivebootcompleted",Toast.LENGTH_LONG).show();
Intentstart=newIntent(context,DisableReceiverInCodeActivity.class);
start.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(start);
}
}
主界面是DisableReceiverInCodeActivity,布局文件中有个checkbox,当checkbox选中,就在开机时启动这个程序,否则不启动。
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="startonbootcomplete">
</TextView>
<CheckBoxandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/boot_complete_checkbox"/>
</LinearLayout>
</LinearLayout>
然后是checkbox的事件和处理
publicclassDisableReceiverInCodeActivityextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
finalCheckBoxbootCheck=(CheckBox)findViewById(R.id.boot_complete_checkbox);
finalComponentNamecm=newComponentName("com.rsft.test.demo1","com.rsft.test.demo1.MyReceiver");
finalPackageManagerpm=getPackageManager();
intstate=pm.getComponentEnabledSetting(cm);
if(state!=PackageManager.COMPONENT_ENABLED_STATE_DISABLED
&&state!=PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER){
bootCheck.setChecked(true);
}
bootCheck
.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener(){
@Override
publicvoidonCheckedChanged(CompoundButtonbuttonView,
booleanisChecked){
intnewState=bootCheck.isChecked()?PackageManager.COMPONENT_ENABLED_STATE_ENABLED
:PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
pm.setComponentEnabledSetting(cm,newState,
PackageManager.DONT_KILL_APP);
}
});
}
}
然后就结束了,简单说一下ComponentNamecm=newComponentName("com.rsft.test.demo1","com.rsft.test.demo1.MyReceiver")这个东西前面是程序的包名,后面是组件的类名,我的是一个receiver,注意类名一定得是全名,就是包名+类名的方式。
再说一下pm.setComponentEnabledSetting(cm,newState,
PackageManager.DONT_KILL_APP);这个就是让一个组件失效或者生效的方法,第一个参数是组件,第二个是你要设置的状态,查查文档就知道有哪些状态,第三个参数可设置的值只有两个,具体看看文档。
工程文件的下载地址我会写在评论里面,欢迎下载。
谢谢阅读
若水飞天
相关文章推荐
- Android根据用户设置决定是否在开机时启动应用程序
- Android根据用户设置决定是否在开机时启动应用程序
- Android根据用户设置决定是否在开机时是否启动
- 浅谈Android开机启动速度优化(含应用程序启动速度优化
- Android开机自启动程序设置及控制方法
- android 将自己的APP 设置为开机启动,不闪桌面
- Android开发 设置开机自动启动
- realARM s5pv210开发板wifi开机启动设置方法 开机启动linux应用程序
- C#.NET Form设置/取消开机自动运行,判断程序是否已经设置成开机自动启动
- ubuntu设置开机启动图形应用程序,替换默认图形桌面
- Android设置全局变量&&启动系统自带应用程序
- Android开发 设置开机自动启动
- Centos平台Supervisord全攻略(docker应用程序设置为开机启动)
- 如何在注册表中设置某项应用程序为开机时自动启动?
- Android 小米手机是否需要设置启动模式
- 浅谈Android开机启动速度优化(含应用程序启动速度优化)
- ubuntu设置开机启动图形应用程序,替换默认图形桌面 【转】
- Android App 开机启动画面和开机自动启动APP程序设置
- 浅谈Android开机启动速度优化(含应用程序启动速度优化)
- android开发设置应用开机自启动