標籤:
Activity向廣播接收器傳遞資料很簡單,只需要在發送廣播前將資料put進Intent中就行了。
廣播接收器怎麼向Activity傳送資料?這裡要用到介面,通過在廣播接收器裡定義一個介面,然後讓接收廣播接收器資料的Activity實現這個介面。先看下面的栗子,Activity發送一個廣播,然後廣播接收器返回一個字串。
Activity布局檔案
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 tools:context="com.nangch.broadcastreceivertest.MainActivity"> 8 9 <TextView10 android:id="@+id/tv"11 android:layout_width="wrap_content"12 android:layout_height="wrap_content"13 android:text="hello" />14 15 <Button16 android:id="@+id/btn"17 android:layout_width="match_parent"18 android:layout_height="wrap_content"19 android:text="發送廣播"/>20 </LinearLayout>
Activity代碼
1 import android.content.Intent; 2 import android.content.IntentFilter; 3 import android.os.Bundle; 4 import android.support.v7.app.AppCompatActivity; 5 import android.view.View; 6 import android.widget.Button; 7 import android.widget.TextView; 8 9 public class MainActivity extends AppCompatActivity implements MyReceiver.Message{10 11 TextView tv;12 MyReceiver myReceiver;13 14 @Override15 protected void onCreate(Bundle savedInstanceState) {16 super.onCreate(savedInstanceState);17 setContentView(R.layout.activity_main);18 19 //註冊廣播接收器20 myReceiver = new MyReceiver();21 IntentFilter intentFilter = new IntentFilter();22 intentFilter.addAction("com.nangch.broadcasereceiver.MYRECEIVER");23 registerReceiver(myReceiver, intentFilter);24 25 //因為這裡需要注入Message,所以不能在AndroidManifest檔案中靜態註冊廣播接收器26 myReceiver.setMessage(this);27 28 tv = (TextView) findViewById(R.id.tv);29 Button btn = (Button) findViewById(R.id.btn);30 btn.setOnClickListener(new View.OnClickListener() {31 @Override32 public void onClick(View v) {33 Intent intent = new Intent("com.nangch.broadcasereceiver.MYRECEIVER");34 intent.putExtra("hello", tv.getText()); //向廣播接收器傳遞資料35 sendBroadcast(intent); //發送廣播36 }37 });38 }39 40 @Override41 public void getMsg(String str) {42 //通過實現MyReceiver.Message介面可以在這裡對MyReceiver中的資料進行處理43 tv.append(str);44 }45 46 @Override47 protected void onDestroy() {48 super.onDestroy();49 unregisterReceiver(myReceiver); //登出廣播接收器50 }51 }
廣播接收器代碼
1 import android.content.BroadcastReceiver; 2 import android.content.Context; 3 import android.content.Intent; 4 import android.widget.Toast; 5 6 public class MyReceiver extends BroadcastReceiver { 7 private Message message; 8 9 @Override10 public void onReceive(Context context, Intent intent) {11 //接收MainActivity傳過來的資料12 Toast.makeText(context, intent.getStringExtra("hello"), Toast.LENGTH_SHORT).show();13 14 //調用Message介面的方法15 message.getMsg(" world");16 }17 18 interface Message {19 public void getMsg(String str);20 }21 22 public void setMessage(Message message) {23 this.message = message;24 }25 }
如下:
點擊發送廣播按鈕後:
在MyReceiver中定義一個Message介面,並聲明一個Message類型的成員變數message。然後讓MainActivity實現這個介面,並調用setMessage方法將MainActivity注入,這樣MainActivity執行個體就成了Myreceiver的成員變數message,這樣就能處理MyReceiver中的資料了。這種思想和我們學習Android時設定按鈕監聽器的思想差不多,仔細想一下還是很好理解的。
示範執行個體源碼下載
Android廣播接收器和Activity間傳遞資料