广播收到信息传到activity更新fragment
来源:互联网 发布:esp8266与单片机连接 编辑:程序博客网 时间:2024/04/30 18:55
广播与activity的交互可以通过接口回调的方式或者handler
/*********************************************************activity**********************************************/protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); fragment1 = new Fragment1(); ft.add(R.id.f1, fragment1); ft.commit(); mb = new MyBroadCast(); IntentFilter inf = new IntentFilter("com.sharetronic.test.MY_BROADCAST"); registerReceiver(mb, inf); mb.setBRInteractionListener(this); Intent intent = new Intent("com.sharetronic.test.MY_BROADCAST"); sendBroadcast(intent); } @Override public void setText(String text) { if (text!=null){ Toast.makeText(MainActivity.this, "11111111111111", Toast.LENGTH_SHORT).show();// oo.setmesg(text); mhandler.obtainMessage(0x01,text).sendToTarget(); } } public void setHandler(Handler handler){ mhandler =handler; }/*****************************************************broadcast*************************************/public void onReceive(Context context, Intent intent) { Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show(); brInteraction.setText("received in MyBroadcastReceiver");}public interface Brecive{ public void setText(String text);}public void setBRInteractionListener(Brecive brInteraction) { this.brInteraction = brInteraction;}/************************************************fragment**********************************************/@Override public void onAttach(Activity activity) { super.onAttach(activity); mactivity = (MainActivity)activity; mactivity.setHandler(mhandler); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.layout2,null); tv = (TextView) view.findViewById(R.id.tv); return view; } private Handler mhandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case 0x01: String string = (String) msg.obj; Log.d("TAG",string); tv.setText(string);// Toast.makeText(activity, "11111111111111", Toast.LENGTH_SHORT).show(); break; } } };
0 0
- 广播收到信息传到activity更新fragment
- Activity完成任务finish后发送广播更新fragment的bug
- 使用广播 更新Fragment
- [已解决] 收到广播无法启动activity
- Activity向Fragment发送广播
- Activity更新Fragment
- Activity和Fragment通信------发送广播Broadcast
- Android生命周期:Activity,Thread,服务,广播,fragment
- fragment更新activity中的界面
- Service通过广播Broadcast更新Activity UI
- Service通过广播Broadcast更新Activity UI
- Service通过广播Broadcast更新Activity UI
- Service通过广播Broadcast更新Activity UI
- Adapter 更新UI Activity 或者fragment
- Fragment与Activity--数据传递、更新UI
- 在activity中更新fragment中的界面
- Fragment与Activity的信息传递
- Fragment和Activity之间通过广播的方式传递数据
- 安卓三种动画小结
- html中的js获取当前的页面是位于哪个容器中打开(pc,手机,微信...)
- 黑马程序员--线程间的通信
- igrimace v8 一键新机 ios8 抹机 平安易贷 微信陌陌站街 借贷宝
- Linux初学实战之shell
- 广播收到信息传到activity更新fragment
- 《leetCode》:Search in Rotated Sorted Array
- Scala学习第十二天 Scala中的继承:超类的构造、重写字段、重写方法代码实战
- cursor的一些总结
- 贪心算法_乘船问题
- java关键字
- 解决spring使用动态代理类型转换失败的问题--java.lang.ClassCastException: com.sun.proxy.$Proxy$ cannot be cast to ...
- 折线分割平面
- mac compile php for pdo_pgsql