【发布时间】:2013-07-26 09:30:08
【问题描述】:
我正在我的应用程序中使用蓝牙服务,该服务使我能够从另一台设备接收到消息。在我的 FragmentActivity 中,我正在使用处理程序来获取此消息:
片段活动:
public final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
//my code
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
byte[] alpha = null;
alpha=readBuf;
if(alpha!=null){
//my code..
}
}
}
我想从这个处理程序中获取数据并将其传输到片段。 我尝试使用 bundle 但它不起作用..
我试过的代码:
在 FragmentActivity 中:
Intent intent = new Intent();
intent.setClass(getApplicationContext(), General.class);
Bundle bundle=new Bundle();
bundle.putInt("battery", bat);
intent.putExtra("android.intent.extra.INTENT", bundle);
在片段中:
Bundle bundle = getActivity().getIntent().getExtras();
if (bundle != null) {
int mLabel = bundle.getInt("battery", 0);
Toast.makeText(getActivity(), "tottiti: "+mLabel, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getActivity(), "prout", Toast.LENGTH_SHORT).show();
}
应用程序返回“pout”,这意味着它无法从我的 FragmentActivity 获取我的数据。
还有其他方法可以从 fragmentActivity 获取数据并将其传输到 Fragment 吗?
感谢您的帮助
【问题讨论】:
标签: android bundle fragment android-fragmentactivity