Android广播接收器和Activity间传递数据

  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     <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 }

效果图如下:

点击发送广播按钮后:

在MyReceiver中定义一个Message接口,并声明一个Message类型的成员变量message。然后让MainActivity实现这个接口,并调用setMessage方法将MainActivity注入,这样MainActivity实例就成了Myreceiver的成员变量message,这样就能处理MyReceiver中的数据了。这种思想和我们学习Android时设置按钮监听器的思想差不多,仔细想一下还是很好理解的。

演示实例源码下载

原文地址:https://www.cnblogs.com/nangch/p/5353563.html