开始学习BroadcastReceiver时用的是动态注册方式,反馈信息也非常便捷。后来想改为静态注册的方式却不知如何在接收到广播后传递信息到Activity活动页。网上也没找到可以起作用的方法。后来突然想到自己可以在自己的Receiver里写一个方法返回在相应广播里拿到的信息,然后在Activity创建该类的无参构造函数对象,在期望的时间点调用该方法取得相应信息。
下面是示例:
首先创建了一个继承BroadcastReceiver的类MyReceiver并在AndroidManifest.xml文件里增添了三个自订的需要它监听的广播事件
AndroidManifest.xml
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.ACTION1" />
<action android:name="com.ACTION2"/>
<action android:name="com.ACTION3"/>
</intent-filter>
</receiver>
然后在MyReceiver类里写了在接收到相应广播事件后要传递的数据,这里示例传递的是一个String对象,自然也可以写其他的,不过方法的返回值也要变,像这里写了一个getStr()方法获取该String对象并返回String对象
MyReceiver.class
package com.ooo.deemo.mybroadcastgo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
public class MyReceiver extends BroadcastReceiver {
static String ss = "";
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
ss = "I'm outside";
Toast.makeText(context, "onReceive", Toast.LENGTH_SHORT).show();
String action = intent.getAction();
if (action.equals("com.ACTION1")) {
ss = "I'm inside ACTION1";
} else if (action.equals("com.ACTION2")) {
ss = "I'm inside ACTION2";
} else {
//由于在AndroidManifest中静态注册了com.ACTION3,所以该广播会被接收并在这里处理
ss = "I'm inside else(by now , i'm inside ACTION3)";
}
}
//提供一个方法返回处理信息
public static String getStr() {
return ss;
}
}
然后要在活动页里实例化MyReceiver,当用其他三个按钮发送相应的广播后,点击第四个按钮调用getStr()方法获取相应的信息
MainActivity.class
package com.ooo.deemo.mybroadcastgo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bt_go1;
private Button bt_go2;
private Button bt_go3;
private Button bt_done;
private TextView tv_show;
private TextView tv_show2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
//注册组件
private void initView() {
bt_go1 = findViewById(R.id.bt_go1);
tv_show = findViewById(R.id.tv_show);
bt_go2 = findViewById(R.id.bt_go2);
tv_show2 = findViewById(R.id.tv_show2);
bt_go3 = findViewById(R.id.bt_go3);
bt_done = findViewById(R.id.bt_done);
bt_go1.setOnClickListener(this);
bt_go2.setOnClickListener(this);
bt_go3.setOnClickListener(this);
bt_done.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_go1:
//发送指定广播ACTION1
Intent intent = new Intent("com.ACTION1");
sendBroadcast(intent);
break;
case R.id.bt_go2:
//发送指定广播ACTION2
final Intent intent2 = new Intent("com.ACTION2");
sendBroadcast(intent2);
break;
case R.id.bt_go3:
//发送指定广播ACTION3
final Intent intent3 = new Intent("com.ACTION3");
sendBroadcast(intent3);
break;
case R.id.bt_done:
MyReceiver myReceiver = new MyReceiver();
tv_show2.setText(myReceiver.getStr()); // 调用getStr方法获取特定广播信息,并将广播信息展示在textView上
break;
default:
break;
}
}
}
该范例已上传至github