android实现广播机制有两种方法,一种需要在AndroidManifest.xml中注册,一种不需要注册。

先说说需要在AndroidManifest.xml注册的:

第一步,发送广播

public class TestActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */ private Button sendButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.testreceiver); sendButton = (Button) findViewById(R.id.sendButton); sendButton.setOnClickListener(this); } @Override public void onClick(View v) { int viewId = v.getId(); switch (viewId) { case R.id.sendButton: { Log.e("@@@@@", "@@sendBroadcast"); Intent intent = new Intent();//new一个intent说明意图 intent.setAction(Intent.ACTION_EDIT);//intent.setAction()方法说明动作类型,是看,还是编辑等等 TestActivity.this.sendBroadcast(intent);//发送广播 break; } } } }

intent有两个参数,一个是动作类型,一个是数据。就像喝水,喝是动作类型,水是数据。

在发送广播时,只需要定义动作类型就可以。

第二步,在AndroidManifest.xml中注册广播接收器:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.my" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".TestActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!--在这里注册广播接收器--> <!-- .TestReceiver是接收器类的类名。 过滤器intent-filter定义了动作类型为EDIT, 说明这个接收器只有遇到EDIT这个动作才会接收 --> <receiver android:name=".TestReceiver"> <intent-filter> <action android:name="android.intent.action.EDIT" /> </intent-filter> </receiver> </application> </manifest>

第三步,实现广播接收器:

public class TestReceiver extends BroadcastReceiver { /** * 构造一个广播接收器 */ public TestReceiver() { Log.e("@@@@@", "@@TestReceiver"); } /** * 调用构造的广播接收器接收广播 */ @Override public void onReceive(Context context, Intent intent) { Log.e("@@@@@", "@@onReceive"); } }

ok!对于这种在AndroidManifest.xml注册的方式,android不能自动销毁广播接收器,也就是说当应用程序关闭后,广播接收器还是会接收广播,这样就会很麻烦。比如,当前应用程序需要接收广播并会弹出一个消息,当用户关闭应用程序后,广播接收器还会继续接收广播并会弹出消息,这样就影响了用户的使用,所以,第二种广播接收器的实现可以让程序员手动定义销毁接收器的代码。

再来说说不需要在AndroidManifest.xml注册的方式:

在代码中注册广播接收器:

public class TestBC2Activity extends Activity implements OnClickListener { private Button registerButton = null; private Button unregisterButton = null; private SMSReceiver smsReceiver = null; private static final String SMS_ACTION = "android.provider.Telephony.SMS_RECEIVED"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); registerButton = (Button) findViewById(R.id.register); registerButton.setOnClickListener(this); unregisterButton = (Button) findViewById(R.id.unregister); unregisterButton.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub int viewId = v.getId(); switch (viewId) { case R.id.register: { smsReceiver = new SMSReceiver();//生成一个BroiadcastReceiver对象 IntentFilter filter = new IntentFilter();//生成一个IntentFilter对象 filter.addAction(SMS_ACTION);//为IntentFilter添加一个Action TestBC2Activity.this.registerReceiver(smsReceiver, filter);//将BroadcastReceiver对象注册到系统当中 break; } case R.id.unregister: { TestBC2Activity.this.unregisterReceiver(smsReceiver);//解除BroadcastReceiver对象的注册 break; } } } }

分别定义了两个按钮,用来注册和解除注册。

下面的内容就和第一种方式一样了。

更多相关文章

  1. Drawable、Bitmap、byte[]之间的转换
  2. Android系统默认Home应用程序(Launcher)的启动过程源代码分析(4)
  3. Android中传感器的一些功能
  4. Intent在Android中的几种用法[转]
  5. android http请求访问接口的封装
  6. android:app接收adb发送的命令并显示
  7. Android(安卓)部分系统广播
  8. 基于 Android NDK 的学习之旅-----数据传输二(引用数据类型)(附源

随机推荐

  1. 模拟android访问服务器
  2. Android(安卓)anr异常测试
  3. Android(安卓)Material Design 实践(三)-
  4. android webview无网络情况下的处理
  5. Android(安卓)Studio 自带的侧滑布局设置
  6. Android(安卓)switch 开关(兼容)
  7. android Gallery 详解
  8. android:HttpURLConnection
  9. android视频聊天源码下载(wifi)
  10. Android中onSaveInstanceState和onRestor