android中BroadcastReceiver 收到的数据如何传递给前台的Activity显示 15
android中BroadcastReceiver收到的数据如何传递给前台的Activity显示...
android中BroadcastReceiver 收到的数据如何传递给前台的Activity显示
展开
2个回答
展开全部
用接口传。定义一个接口 让 Activity实现这个接口,然后接受者调用接口里面的方法把要传递的参数传进去。
例如用broadcastreceiver获得android手机的电池电量,并把电量显示到activity中:
广播接收者子类
package com.ht.dianliang;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
/**
* Created by annuo on 2015/5/16.
*/
public class DianLiangBR extends BroadcastReceiver {
private BRInteraction brInteraction;
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
//获取当前电量
int current = bundle.getInt("level");
//获取总电量(电池的电池容量)
int total = bundle.getInt("scale");
brInteraction.setText("当前电量:" + current + ",总电量:" + total);
}
public interface BRInteraction {
public void setText(String content);
}
public void setBRInteractionListener(BRInteraction brInteraction) {
this.brInteraction = brInteraction;
}
}
activity代码
package com.ht.dianliang;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity implements DianLiangBR.BRInteraction {
private TextView textView;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.text);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
DianLiangBR dianLiangBR = new DianLiangBR();
registerReceiver(dianLiangBR, intentFilter);
dianLiangBR.setBRInteractionListener(this);
}
@Override
public void setText(String content) {
if (content != null) {
textView.setText(content);
}
}
}
例如用broadcastreceiver获得android手机的电池电量,并把电量显示到activity中:
广播接收者子类
package com.ht.dianliang;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
/**
* Created by annuo on 2015/5/16.
*/
public class DianLiangBR extends BroadcastReceiver {
private BRInteraction brInteraction;
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
//获取当前电量
int current = bundle.getInt("level");
//获取总电量(电池的电池容量)
int total = bundle.getInt("scale");
brInteraction.setText("当前电量:" + current + ",总电量:" + total);
}
public interface BRInteraction {
public void setText(String content);
}
public void setBRInteractionListener(BRInteraction brInteraction) {
this.brInteraction = brInteraction;
}
}
activity代码
package com.ht.dianliang;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity implements DianLiangBR.BRInteraction {
private TextView textView;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.text);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
DianLiangBR dianLiangBR = new DianLiangBR();
registerReceiver(dianLiangBR, intentFilter);
dianLiangBR.setBRInteractionListener(this);
}
@Override
public void setText(String content) {
if (content != null) {
textView.setText(content);
}
}
}
展开全部
你完全可以在你的activity中动态注册一个BroadcastReceiver ,这样BroadcastReceiver接受到的数据不就直接保存在你的Activity中了吗,想咋显示都行了!
更多追问追答
追问
我的BroadcastReceiver是新的一个继承BroadcastReceiver
的一个类
追答
我知道啊,我是说你完全没有必要这样,完全没有必要新设计一个BroadcastReceiver类,在Activity中动态注册不就一切问题都没了
像你这样情况要把数据传递给activity还是要通过发广播,这样还是必须在activity中注册一个动态的BroadcastReceiver类,何必呢,!
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询