編輯:Android編程入門
在Android中,Broadcast是一種廣泛運用的在應用程序之間傳輸信息的機制。而BroadcastReceiver是對發送出來的 Broadcast進行過濾接受並響應的一類組件。
目錄導航:
一、 廣播的種類:標准廣播和有序廣播
二、 注冊BroadcastReceiver的兩種方式:
<receiver android:name=".NetworkChangeReceiver"> <intent-filter> <action android:name="com.example.linux.broadcasttest.MyBroadcast" /> </intent-filter> </receiver>
Intent intent = new Intent(); intent.putExtra("username", "linux"); intent.setAction("com.example.linux.broadcasttest.MyBroadcast"); sendBroadcast(intent);
我們建立一個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); }
二、 創建三個廣播接收器,用於廣播的測試:它們分別如下
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); } }
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(); } }
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>
一、 定義方法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
1.5版本的android studio上傳本地項目到git@osc上:1、在git上創建一個項目;2、在studio上導入git上新創建的項目:把git上的地址復制下來
1. 輪播控件的組成部分 我們以知乎日報Android客戶端的輪播控件為例,分析一下輪播控件的主要組成: &
引言 通過前面兩篇: Android 開發之旅:環境搭建及HelloWorld Android 開發之旅:HelloWorld項
一、概述1. 什麼是Handler Handler是Android消息機制的上層接口,它為我們封裝了許多底層的細節,讓我們能夠很方便的使用底層的