效果圖示例:java
//負責發送廣播的應用android
一、佈局只有一個發送廣播按鈕app
二、在配置清單裏添加權限(自定義)ide
代碼佈局
<?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>
三、MainActivity.java類spa
代碼日誌
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"); } }
==================================================code
發送廣播端作好後作另外應用的廣播接收端(跨應用)xml
//另外的應用(廣播接收者)有2個有序廣播接收者 把接收到的結果打印到日誌貓繼承
//廣播接收者類都要繼承BroadCastReceiver類
四、在配置清單裏靜態註冊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>
五、配置清單配置好後編寫源文件有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);//把結果打印到日誌貓 } }
六、最後先運行廣播接收者的應用(部署好廣播接收者),在運行廣播發送端發送廣播 最後在日誌貓查看打印信息