編輯:關於 Android 程式設計
廣播(Broadcast)是 Android 元件之間解耦溝通的常用手段。本文用一個最小範例,說明如何在 Activity 中完成「定義接收器 → 註冊 → 發送」這一整條流程。
繼承 BroadcastReceiver 並覆寫 onReceive(),在裡面比對 action,決定要做什麼:
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ACTION_NAME)) {
Toast.makeText(Test.this, "處理 action 名字相對應的廣播", 200).show();
}
}
};
建立 IntentFilter,把感興趣的 action 加進去,再呼叫 registerReceiver():
public void registerBoradcastReceiver() {
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction(ACTION_NAME);
// 註冊廣播
registerReceiver(mBroadcastReceiver, myIntentFilter);
}
用 Intent 帶上 action,需要的話再用 putExtra() 塞入要傳遞的資料,最後 sendBroadcast():
mBtnMsgEvent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent mIntent = new Intent(ACTION_NAME);
mIntent.putExtra("yaner", "發送廣播,相當於在這裡傳送數據");
// 發送廣播
sendBroadcast(mIntent);
}
});
package my.yaner;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Test extends Activity {
private final String ACTION_NAME = "yaner";
private Button mBtnMsgEvent = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 註冊廣播
registerBoradcastReceiver();
LinearLayout mLinearLayout = new LinearLayout(this);
mBtnMsgEvent = new Button(this);
mBtnMsgEvent.setText("發送廣播");
mLinearLayout.addView(mBtnMsgEvent);
setContentView(mLinearLayout);
mBtnMsgEvent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent mIntent = new Intent(ACTION_NAME);
mIntent.putExtra("yaner", "發送廣播,相當於在這裡傳送數據");
sendBroadcast(mIntent);
}
});
}
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ACTION_NAME)) {
Toast.makeText(Test.this, "處理 action 名字相對應的廣播", 200).show();
}
}
};
public void registerBoradcastReceiver() {
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction(ACTION_NAME);
registerReceiver(mBroadcastReceiver, myIntentFilter);
}
}
幾個實務提醒:動態註冊的廣播記得在 onDestroy() 中呼叫 unregisterReceiver() 反註冊,否則容易造成記憶體洩漏;靜態廣播則要寫進 AndroidManifest.xml。另外從 Android 8.0 起,大部分隱含 Intent 的靜態廣播已被限制,自訂 action 建議一律走動態註冊或改用其他元件溝通機制。
Android 6.0 應用啟動全流程源碼深度剖析
以下分析基於 API 23(Android 6.0)。想真正搞懂一個 App 是怎麼從桌面圖標被點亮、一路走到 Activity 呈現在眼前的,那就得潛進源碼,順著調用鏈
把 Android 開源專案發佈到 JCenter 的完整流程與踩坑筆記
我們每天都在 build.gradle 裡寫 compile com.android.support:design:23.4.0,一行字就把別人的成果拉進自己的專案。那反
Android 資料傳遞筆記(三):用介面回呼把 Activity 的資料送進 Fragment
Activity 與 Fragment 之間要交換資料,做法其實不少,但最常見、也最能體現解耦思想的一種,就是「介面回呼」(Interface Callback)。本文用
Android 緩存實戰:二級緩存概念與輕量框架 ACache
一、為什麼需要緩存 Android 開發的本質,很大程度上就是手機與遠端伺服器之間的資料往返。反覆透過網路取資料的代價很高,一旦請求頻繁,效能與流量都會同時惡化。 解法是