广播分为普通广播、有序广播、异步广播,异步广播和普通广播类似,广播不能终止和处理,有序广播可终止可处理,并且三种都是级别高的先接收广播。
目标效果:
点击第一个按钮发送的是普通广播,第二个按钮发送的是有序广播,因为普通广播不可以处理,所以第一个按钮点击后处理结果为null。
1.activity_main.xml页面放置两个按钮。
activity_main.xml页面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/btSendOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="doClick"
android:text="发送一条普通广播" />
<Button
android:id="@+id/btSendTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btSendOne"
android:onClick="doClick"
android:text="发送一条有序广播" />
</RelativeLayout>
2.新建broad.java页面作为第一个接收广播的页面。
broad.java页面:
package com.example.broad;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class broad extends BroadcastReceiver{
@Override
public void onReceive(Context content, Intent intent) {
String s=intent.getStringExtra("sendOne");
Log.i("MainActivity","收到消息1:s="+s);
/*处理广播*/
Bundle bundle=new Bundle();
bundle.putString("test","广播处理的数据");
setResultExtras(bundle);//是否得到返回值
}
}
3.新建broadSecond.java页面作为第二个接收广播的页面。
broadSecond.java页面:
package com.example.broad;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class broadSecond extends BroadcastReceiver{
@Override
public void onReceive(Context content, Intent intent) {
String s=intent.getStringExtra("sendOne");
Log.i("MainActivity","收到消息2:s="+s);
//abortBroadcast(); //拦截广播,只对有序广播起作用
Bundle bundle=getResultExtras(true);
String s2=bundle.getString("test");
Log.i("MainActivity","得到的处理结果:s2="+s2);
}
}
4.MainActivity.java页面用于发送广播。
MainActivity.java页面:
package com.example.broad;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btSendOne,btSendTwo;
private Intent intentOne,intentTwo;
private broad one;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getId();//获取控件id
register();//注册广播
}
private void getId() {
btSendOne=(Button) findViewById(R.id.btSendOne);
btSendTwo=(Button) findViewById(R.id.btSendTwo);
}
public void doClick(View view){
switch (view.getId()) {
case R.id.btSendOne://发送一条普通广播
intentOne=new Intent();
intentOne.putExtra("sendOne","这是一条普通广播");
intentOne.setAction("Main");//过滤器
sendBroadcast(intentOne);//发送
break;
case R.id.btSendTwo:
intentTwo=new Intent();
intentTwo.putExtra("sendOne","这是一条有序广播");
intentTwo.setAction("Main");//过滤器
sendOrderedBroadcast(intentTwo,null);//发送,参数二为权限
break;
}
}
/*动态注册广播接收器,优先级高于静态注册,只有在程序运行时生效*/
private void register() {
IntentFilter intentfilter=new IntentFilter("Main");
one=new broad();
registerReceiver(one,intentfilter);
}
/*动态注册的广播需要销毁*/
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(one);
}
}
5.广播都需要注册,注册有动态和静态两种方式,第一个广播是使用了动态注册,第二个广播使用静态注册,所以需要在AndroidManifest.xml页面进行注册。
AndroidManifest.xml页面:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broad"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.BROADCAST_STICKY"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.broad.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 静态注册接收器 -->
<receiver android:name="com.example.broad.broadSecond" >
<intent-filter>
<!-- android:priority=""是优先级,优先级高的先执行 -->
<action android:name="Main" />
</intent-filter>
</receiver>
</application>
</manifest>
6.同等级别的广播,动态注册优先级高于静态注册,静态注册时也可以自定义设置优先级,取值范围为1-1000。
<intent-filter
android:priority="100">
<!-- android:priority=""是优先级,优先级高的先执行 -->
<action android:name="Main" />
</intent-filter>
7.最后还需要添加权限。
8.运行就可以显示目标效果了。