Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android編程入門 >> android基礎---->Broadcast的使用

android基礎---->Broadcast的使用

編輯:Android編程入門

  在Android中,Broadcast是一種廣泛運用的在應用程序之間傳輸信息的機制。而BroadcastReceiver是對發送出來的 Broadcast進行過濾接受並響應的一類組件。

 

目錄導航:

  1.  廣播Broadcast的簡要說明
  2.  廣播Broadcast的准備工作
  3.  Broadcast廣播的測試
  4.  友情鏈接

 

廣播Broadcast的簡要說明

一、 廣播的種類:標准廣播和有序廣播

  • 標准廣播:是一種完全異步執行的廣播,在廣播發出之後,所有的廣播接收器幾乎都會在同一時刻接收到這條廣播消息,因此它們之間沒有任何先後順序可言。這種廣播的效率會比較高,但同時也意味著它是無法被截斷的

   

  • 有序廣播:則是一種同步執行的廣播,在廣播發出之後,同一時刻只會有一個廣播接收器能夠收到這條廣播消息,當這個廣播接收器中的邏輯執行完畢後,廣播才會繼續傳遞

  

二、 注冊BroadcastReceiver的兩種方式:

  • 靜態注冊:在AndroidManifest.xml中用標簽生命注冊,並在標簽內用標簽設置過濾器
<receiver android:name=".NetworkChangeReceiver">
    <intent-filter>
        <action android:name="com.example.linux.broadcasttest.MyBroadcast" />
    </intent-filter>
</receiver>
  •  動態注冊:在java代碼中調用registerReceiver(BroadcastReceiver receiver, IntentFilter filter)方法
Intent intent = new Intent();
intent.putExtra("username", "linux");
intent.setAction("com.example.linux.broadcasttest.MyBroadcast");
sendBroadcast(intent);

 

廣播Broadcast的准備工作

我們建立一個android項目,結構如下:

 

一、 在MainActivity中初始化一些信息:

private final static String TAG = "OtherReceiver";
private IntentFilter intentFilter;
private NetworkChangeReceiver networkChangeReceiver;
private LocalBroadcastManager localBroadcastManager;
private LocalReceiver localReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "main on create");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

 

二、 創建三個廣播接收器,用於廣播的測試:它們分別如下

  • LocalReceiver本地廣播:
package com.example.linux.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by Linux on 2016/3/7.
 */
public class LocalReceiver extends BroadcastReceiver {
    private final static String TAG = "OtherReceiver";

    public LocalReceiver() {
        Log.i(TAG, "local receiver constructor.");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String username = intent.getStringExtra("username");
        Log.i(TAG, "local username: " + username);
    }
}
  • OtherReceiver用於測試有序廣播:
package com.example.linux.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class OtherReceiver extends BroadcastReceiver {
    private final static String TAG = "OtherReceiver";
    public OtherReceiver() {
        Log.i(TAG, "other receiver constructor.");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String username = intent.getStringExtra("username");
        Log.i(TAG, "other username: " + username);
        abortBroadcast();
    }
}
  • NetworkChangeReceiver用於測試系統廣播的使用:
package com.example.linux.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by Linux on 2016/3/7.
 */
public class NetworkChangeReceiver extends BroadcastReceiver {
    private final static String TAG = "OtherReceiver";

    public NetworkChangeReceiver() {
        Log.i(TAG, "network receiver constructor.");
    }

    @Override
    //Intent是sendBroadcast中的參數
    public void onReceive(Context context, Intent intent) {
        String username = intent.getStringExtra("username");
        Log.i(TAG, "network username: " + username);
    }
}

 

三、 由於廣播會涉及到注冊,這裡我把廣播的注冊代碼貼出來:在AndroidManifest.xml文件中

<receiver android:name=".NetworkChangeReceiver">
    <intent-filter>
        <action android:name="com.example.linux.broadcasttest.MyBroadcast" />
    </intent-filter>
</receiver>
<receiver android:name=".OtherReceiver">
    <intent-filter android:priority="100">
        <action android:name="com.example.linux.broadcasttest.MyBroadcast" />
    </intent-filter>
</receiver>
<receiver android:name=".LocalReceiver">
    <intent-filter>
        <action android:name="com.example.linux.broadcasttest.MyBroadcast" />
    </intent-filter>
</receiver>

 

Broadcast廣播的測試

一、 定義方法sendSystemBroad,用於發送系統廣播

// 發送系統廣播
public void sendSystemBroad(View view) {
    intentFilter = new IntentFilter();
    intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
    networkChangeReceiver = new NetworkChangeReceiver();
    registerReceiver(networkChangeReceiver, intentFilter);
}
  • 打印的結果:
03-23 23:03:20.087 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: other receiver constructor.
03-23 23:03:20.087 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: other username: linux
03-23 23:03:20.097 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: network receiver constructor.
03-23 23:03:20.107 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: network username: linux
03-23 23:03:20.107 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local receiver constructor.
03-23 23:03:20.107 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local username: linux

 

二、 定義方法sendBroadCast,用於測試發送標准廣播

// 發送標准廣播
public void sendBroadCast(View view) {
    Intent intent = new Intent();
    intent.putExtra("username", "linux");
    intent.setAction("com.example.linux.broadcasttest.MyBroadcast");
    sendBroadcast(intent);
}
  • 打印的結果:
03-23 23:05:47.957 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: network receiver constructor.
03-23 23:05:47.967 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: network username: null

 

三、 定義方法sendOrderBroad,用於測試有序廣播

// 發送有序廣播
public void sendOrderBroad(View view) {
    Intent intent = new Intent();
    intent.putExtra("username", "linux");
    intent.setAction("com.example.linux.broadcasttest.MyBroadcast");
    sendOrderedBroadcast(intent, null);
}
  • 打印的結果:
03-23 23:07:28.877 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: other receiver constructor.
03-23 23:07:28.877 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: other username: linux

 

四、 定義方法sendLocalBroad,用於測試本地廣播

// 使用本地廣播
public void sendLocalBroad(View view) {
    localBroadcastManager = LocalBroadcastManager.getInstance(this);
    Intent intent = new Intent();
    intent.putExtra("username", "linux");
    intent.setAction("com.example.linux.broadcasttest.MyBroadcast");
    localBroadcastManager.sendBroadcast(intent);
}
  • 打印的結果:
03-23 23:28:09.327 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local username: linux
03-23 23:28:09.327 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local username: linux
03-23 23:28:09.327 31259-31259/com.example.linux.broadcasttest I/OtherReceiver: local username: linux

 

友情鏈接

  • 廣播的測試源代碼      訪問密碼 1e16
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved