android broadcasttReceiver 一直死循环啊

Android BroadcastReceiver广播注册方式总结
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了Android BroadcastReceiver广播注册方式总结的相关资料,需要的朋友可以参考下
Android:BroadcastReceiver注册的方式分为两种:
1. 静态注册
在AndroidManifest.xml里通过标签声明
属性说明:
android:enabled=["true" | "false"]
//此broadcastReceiver能否接收其他App的发出的广播
//默认值是由receiver中有无intent-filter决定的:如果有intent-filter,默认值为true,否则为false
android:exported=["true" | "false"]
android:icon="drawable resource"
android:label="string resource"
//继承BroadcastReceiver子类的类名
android:name=".mBroadcastReceiver"
//具有相应权限的广播发送者发送的广播才能被此BroadcastReceiver所接收;
android:permission="string"
//BroadcastReceiver运行所处的进程
//默认为app的进程,可以指定独立的进程
//注:Android四大基本组件都可以通过此属性指定自己的独立进程
android:process="string" &
//用于指定此广播接收器将接收的广播类型
//本示例中给出的是用于接收网络状态改变时发出的广播
&intent-filter&
&action android:name="android.net.conn.CONNECTIVITY_CHANGE" /&
&/intent-filter&
&/receiver&
//此广播接收者类是mBroadcastReceiver
android:name=".mBroadcastReceiver" &
//用于接收网络状态改变时发出的广播
&intent-filter&
&action android:name="android.net.conn.CONNECTIVITY_CHANGE" /&
&/intent-filter&
&/receiver&
当此App首次启动时,系统会自动实例化mBroadcastReceiver类,并注册到系统中。
2. 动态注册
在代码中通过调用Context的registerReceiver()方法进行动态注册BroadcastReceiver,具体代码如下:
protected void onResume(){
super.onResume();
//实例化BroadcastReceiver子类 & IntentFilter
mBroadcastReceiver mBroadcastReceiver = new mBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
//设置接收广播的类型
intentFilter.addAction(android.net.conn.CONNECTIVITY_CHANGE);
//调用Context的registerReceiver()方法进行动态注册
registerReceiver(mBroadcastReceiver, intentFilter);
//注册广播后,要在相应位置记得销毁广播
//即在onPause() 中unregisterReceiver(mBroadcastReceiver)
//当此Activity实例化时,会动态将MyBroadcastReceiver注册到系统中
//当此Activity销毁时,动态注册的MyBroadcastReceiver将不再接收到相应的广播。
protected void onPause() {
super.onPause();
//销毁在onResume()方法中的广播
unregisterReceiver(mBroadcastReceiver);
3. 两种注册方式的区别
4. 特别注意
动态广播最好在Activity的onResume()注册、onPause()注销。
1.对于动态广播,有注册就必然得有注销,否则会导致内存泄露
重复注册、重复注销也不允许
2.Activity生命周期如下:
Activity生命周期的方法是成对出现的:
- onCreate() & onDestory()
- onStart() & onStop()
- onResume() & onPause()
在onResume()注册、onPause()注销是因为onPause()在App死亡前一定会被执行,从而保证广播在App死亡前一定会被注销,从而防止内存泄露。
1.不在onCreate() & onDestory() 或 onStart() & onStop()注册、注销是因为:
当系统因为内存不足(优先级更高的应用需要内存,请看上图红框)要回收Activity占用的资源时,Activity在执行完onPause()方法后就会被销毁,有些生命周期方法onStop(),onDestory()就不会执行。当再回到此Activity时,是从onCreate方法开始执行。 2.假设我们将广播的注销放在onStop(),onDestory()方法里的话,有可能在Activity被销毁后还未执行onStop(),onDestory()方法,即广播仍还未注销,从而导致内存泄露。3. 但是,onPause()一定会被执行,从而保证了广播在App死亡前一定会被注销,从而防止内存泄露。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具可以多个广播一个(BroadcastReceiver)接收吗?怎么实现的?
可以多个广播一个(BroadcastReceiver)接收吗?怎么实现的?
写下你的评论...
Copyright (C)
All Rights Reserved | 京ICP备 号-2Android的BroadcastReceiver 发送广播,接收方未启动,会不会等到启动后再接收呢? - 知乎8被浏览2380分享邀请回答03 条评论分享收藏感谢收起巧用BroadcastReceiver实现开机“自”启动
在使用智能手机的时候,有些程序是一直伴随这我们的,或者说是需要实时反馈和交互的,例如我们手机的主题界面,闹钟程序等等。对于这些程序,我们自然而然的会希望他们能够开机自启动,因为这样子可以避免忘记手动开启某些程序,例如日常闹钟等等,并且省了很多繁琐的事情。
&&&&&&& 正如高焕堂先生总结框架时所说的&Don'tcall me, I'll call you back!&,在Android中,不同之间的调用往往是基于消息触发,而不是简单的事件调用。在Android中,广播机制也很好的贯彻了这个思想。下面这个程序,将会演示如何利用BroadcastReceiver来实现Activity和Service的开机自启动。
实现原理:当Android启动时,会发出一个广播,内容为ACTION_BOOT_COMPLETED,它的字符串常量表示为android.intent.action.BOOT_COMPLETED。所以,只要在BroadcastReceiver接收到该消息时,创建并启动相应的Activity和Service即可实现。
在该程序中,将创建一个BroadcastReceiver类BootBroadcastReceiver、一个Activity类StartOnBootActivity、一个Service类StartOnBootService。程序的示例程序的代码如下,重点代码的注解见代码中注释部分:
(1)配置文件&AndroidManifest.xml&
[html] &?xml version=&1.0& encoding=&utf-8&?&&
&manifest xmlns:android=&&&
&&& package=&lulicheng.android.onboot&&
&&& android:versionCode=&1&&
&&& android:versionName=&1.0& &&
&&& &uses-permission android:name=&android.permission.RECEIVE_BOOT_COMPLETED& &&
&&& &/uses-permission&&
&&& &uses-sdk android:minSdkVersion=&10& /&&
&&& &application&
&&&&&&& android:icon=&@drawable/ic_launcher&&
&&&&&&& android:label=&@string/app_name& &&
&&&&&&& &activity&
&&&&&&&&&&& android:name=&.StartOnBootActivity&&
&&&&&&&&&&& android:label=&@string/app_name& &&
&&&&&&&&&&& &intent-filter&&
&&&&&&&&&&&&&&& &action android:name=&android.intent.action.MAIN& /&&
&&&&&&&&&&&&&&& &category android:name=&android.intent.category.LAUNCHER& /&&
&&&&&&&&&&& &/intent-filter&&
&&&&&&& &/activity&&
&&&&&&& &service android:name=&.StartOnBootService& &&
&&&&&&& &/service&&
&&&&&&& &receiver android:name=&.BootBroadcastReceiver& &&
&&&&&&&&&&& &intent-filter&&
&&&&&&&&&&&&&&& &action android:name=&android.intent.action.BOOT_COMPLETED& /&&
&&&&&&&&&&& &/intent-filter&&
&&&&&&& &/receiver&&
&&& &/application&&
&/manifest&&
&?xml version=&1.0& encoding=&utf-8&?&
&manifest xmlns:android=&&
&&& package=&lulicheng.android.onboot&
&&& android:versionCode=&1&
&&& android:versionName=&1.0& &
&&& &uses-permission android:name=&android.permission.RECEIVE_BOOT_COMPLETED& &
&&& &/uses-permission&
&&& &uses-sdk android:minSdkVersion=&10& /&
&&& &application
&&&&&&& android:icon=&@drawable/ic_launcher&
&&&&&&& android:label=&@string/app_name& &
&&&&&&& &activity
&&&&&&&&&&& android:name=&.StartOnBootActivity&
&&&&&&&&&&& android:label=&@string/app_name& &
&&&&&&&&&&& &intent-filter&
&&&&&&&&&&&&&&& &action android:name=&android.intent.action.MAIN& /&
&&&&&&&&&&&&&&& &category android:name=&android.intent.category.LAUNCHER& /&
&&&&&&&&&&& &/intent-filter&
&&&&&&& &/activity&
&&&&&&& &service android:name=&.StartOnBootService& &
&&&&&&& &/service&
&&&&&&& &receiver android:name=&.BootBroadcastReceiver& &
&&&&&&&&&&& &intent-filter&
&&&&&&&&&&&&&&& &action android:name=&android.intent.action.BOOT_COMPLETED& /&
&&&&&&&&&&& &/intent-filter&
&&&&&&& &/receiver&
&&& &/application&
&/manifest&
在该配置文件中,配置了各个组件的基本参数,在使用权限中需要加入&&uses-permissionandroid:name=&android.permission.RECEIVE_BOOT_COMPLETED& &&权限,另外还有一点比较重要的就是在BootBroadcastReceiver中添加intent-filter,如此一来BootBroadcastReceiver的onReceiver方法才能被触发。
(2)广播监听类&BootBroadcastReceiver&
[java] package lulicheng.android.&
import android.content.BroadcastR&
import android.content.C&
import android.content.I&
public class BootBroadcastReceiver extends BroadcastReceiver {&
&&& // 系统启动完成&&
&&& static final String ACTION = &android.intent.action.BOOT_COMPLETED&;&
&&& @Override&
&&& public void onReceive(Context context, Intent intent) {&
&&&&&&& // 当收听到的事件是&BOOT_COMPLETED&时,就创建并启动相应的Activity和Service&&
&&&&&&& if (intent.getAction().equals(ACTION)) {&
&&&&&&&&&&& // 开机启动的Activity&&
&&&&&&&&&&& Intent activityIntent = new Intent(context, StartOnBootActivity.class);&
&&&&&&&&&&& activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);&
&&&&&&&&&&& // 启动Activity&&
&&&&&&&&&&& context.startActivity(activityIntent);&
&&&&&&&&&&& // 开机启动的Service&&
&&&&&&&&&&& Intent serviceIntent = new Intent(context, StartOnBootService.class);&
&&&&&&&&&&& // 启动Service&&
&&&&&&&&&&& context.startService(serviceIntent);&
&&&&&&& }&
(3)&StartOnBootActivity&&
package lulicheng.android.&
import android.app.A&
import android.os.B&
import android.widget.TextV&
public class StartOnBootActivity extends Activity {&
&&& @Override&
&&& public void onCreate(Bundle savedInstanceState) {&
&&&&&&& super.onCreate(savedInstanceState);&
&&&&&&& // 设置Activity的显示内容为一个文本域&&
&&&&&&& TextView aTextView = new TextView(this);&
&&&&&&& aTextView.setText(&Wow!I started after cellphone boot.&);&
&&&&&&& setContentView(aTextView);&
(4)&StartOnBootService&&
package lulicheng.android.&
import android.app.S&
import android.content.I&
import android.os.IB&
import android.widget.T&
public class StartOnBootService extends Service {&
&&& @Override&
&&& public IBinder onBind(Intent intent) {&
&&& @Override&
&&& public void onStart(Intent intent, int startId) {&
&&&&&&& super.onStart(intent, startId);&
&&&&&&& // Service被启动时,将会有弹出消息提示[MyService onStart]&&
&&&&&&& Toast.makeText(this, &[MyService onStart]&, Toast.LENGTH_LONG).show();&
package lulicheng.android.
import android.content.BroadcastR
import android.content.C
import android.content.I
public class BootBroadcastReceiver extends BroadcastReceiver {
&// 系统启动完成
&static final String ACTION = &android.intent.action.BOOT_COMPLETED&;
&@Override
&public void onReceive(Context context, Intent intent) {
&&// 当收听到的事件是&BOOT_COMPLETED&时,就创建并启动相应的Activity和Service
&&if (intent.getAction().equals(ACTION)) {
&&&// 开机启动的Activity
&&&Intent activityIntent = new Intent(context, StartOnBootActivity.class);
&&&activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
&&&// 启动Activity
&&&context.startActivity(activityIntent);
&&&// 开机启动的Service
&&&Intent serviceIntent = new Intent(context, StartOnBootService.class);
&&&// 启动Service
&&&context.startService(serviceIntent);
(3)&StartOnBootActivity&
package lulicheng.android.
import android.app.A
import android.os.B
import android.widget.TextV
public class StartOnBootActivity extends Activity {
&@Override
&public void onCreate(Bundle savedInstanceState) {
&&super.onCreate(savedInstanceState);
&&// 设置Activity的显示内容为一个文本域
&&TextView aTextView = new TextView(this);
&&aTextView.setText(&Wow!I started after cellphone boot.&);
&&setContentView(aTextView);
(4)&StartOnBootService&
package lulicheng.android.
import android.app.S
import android.content.I
import android.os.IB
import android.widget.T
public class StartOnBootService extends Service {
&@Override
&public IBinder onBind(Intent intent) {
&@Override
&public void onStart(Intent intent, int startId) {
&&super.onStart(intent, startId);
&&// Service被启动时,将会有弹出消息提示[MyService onStart]
&&Toast.makeText(this, &[MyService onStart]&, Toast.LENGTH_LONG).show();
程序实现后,在手机或者模拟器上面安装,然后重启机器,就能在开机进入系统之后看到该Activity界面以及Service的弹出消息。程序的运行截屏如下图:
作者:陆利成}

我要回帖

更多关于 broadcast 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信