效果图示例:
//负责发送广播的应用
1、布局只有一个发送广播按钮
2、在配置清单里添加权限(自定义)
代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcast"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<!-- 自定义一个权限 -->
<permission android:name="com.example.permission.MyReceiver"/>
<uses-permission android:name="com.example.permission.MyReceiver"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
</application>
</manifest>
3、MainActivity.java类
代码
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//发送广播按钮事件监听
public void sendBroad(View view) {
Intent intent = new Intent();
//这里setAction中的action是别的应用接收本应用发送的广播的action(相当于暴露给其他应用的路径)
intent.setAction("com.example.broadcast.MyReceiver");
/*
* intent.putExtra("data", "无序广播发送的数据");
*sendBroadcast(intent);//发送
*/
// 有序广播的发送
intent.putExtra("data", "有序广播发送的数据");
// 有序广播发送的权限在配置清单里自定义(第二个参数)
//发送广播的方法sendOrderedBroadcast
sendOrderedBroadcast(intent, "com.example.permission.MyReceiver");
}
}
==================================================
发送广播端做好后做另外应用的广播接收端(跨应用)
//另外的应用(广播接收者)有2个有序广播接收者 把接收到的结果打印到日志猫
//广播接收者类都要继承BroadCastReceiver类
4、在配置清单里静态注册2个广播接收者和添加权限(该权限是发送广播端自定义的权限)
配置清单代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.orderbroadcast"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<!-- 另外一个应用自定义的权限 -->
<uses-permission android:name="com.example.permission.MyReceiver"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
<!-- priority有序广播范围是-1000到1000 数值越大优先权越高 -->
<receiver android:name=".OrderBroadOne">
<intent-filter android:priority="1">
<action android:name="com.example.broadcast.MyReceiver"/>
</intent-filter>
</receiver>
<receiver android:name=".OrderBroadTwo">
<intent-filter android:priority="2">
<action android:name="com.example.broadcast.MyReceiver"/>
</intent-filter>
</receiver>
</application>
</manifest>
5、配置清单配置好后编写源文件有2个类(2个有序广播接收者)一个OrderBroadOne.java类一个OrderBroadTwo.java类
OrderBroadOne.java类
代码
public class OrderBroadOne extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String data = intent.getStringExtra("data");
Log.i("data", "OrderBroadOne data = " + data);//把结果打印到日志猫
}
}
OrderBroadTwo.java类
代码
public class OrderBroadTwo extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String data = intent.getStringExtra("data");
Log.i("data", "OrderBroadTwo data = " + data);//把结果打印到日志猫
}
}
6、最后先运行广播接收者的应用(部署好广播接收者),在运行广播发送端发送广播 最后在日志猫查看打印信息