Android廣播接收器和Activity間傳遞數據

  Activity向廣播接收器傳遞數據很簡單,只須要在發送廣播前將數據put進Intent中就好了。android

  廣播接收器怎麼向Activity傳送數據?這裏要用到接口,經過在廣播接收器裏定義一個接口,而後讓接收廣播接收器數據的Activity實現這個接口。先看下面的栗子,Activity發送一個廣播,而後廣播接收器返回一個字符串。app

 

Activity佈局文件ide

 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     <TextView
10         android:id="@+id/tv"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="hello" />
14 
15     <Button
16         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     @Override
15     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             @Override
32             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     @Override
41     public void getMsg(String str) {
42         //經過實現MyReceiver.Message接口能夠在這裏對MyReceiver中的數據進行處理
43         tv.append(str);
44     }
45 
46     @Override
47     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     @Override
10     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 }

效果圖以下:this

點擊發送廣播按鈕後:spa

在MyReceiver中定義一個Message接口,並聲明一個Message類型的成員變量message。而後讓MainActivity實現這個接口,並調用setMessage方法將MainActivity注入,這樣MainActivity實例就成了Myreceiver的成員變量message,這樣就能處理MyReceiver中的數據了。這種思想和咱們學習Android時設置按鈕監聽器的思想差很少,仔細想一下仍是很好理解的。code

 

演示實例源碼下載xml

相關文章
相關標籤/搜索