android广播的两种方式!

本文详细介绍了Android中的广播机制,包括两种实现方式:一种需要在AndroidManifest.xml中注册,另一种则在代码中动态注册。文中提供了具体的实现代码示例。

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; } } } }

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

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

转载于:https://www.cnblogs.com/boyupeng/archive/2011/03/28/2028537.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值