Android 设置后台静态广播和activity进行通信
来源:互联网 发布:中国青少年肥胖率数据 编辑:程序博客网 时间:2024/05/21 21:27
一:注册设置后台静态广播
import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private Button mButton; private TextView mTextView; public static String ACTION_INTENT_TEST = "com.demo.test"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.message_tv); mButton = (Button) findViewById(R.id.send_btn); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent mIntent = new Intent(ACTION_INTENT_TEST); sendBroadcast(mIntent); } }); registerMessageReceiver(); } //在销毁时要与广播解绑 @Override protected void onDestroy() { unregisterReceiver(mMessageReceiver); super.onDestroy(); } public MessageReceiver mMessageReceiver; public static String ACTION_INTENT_RECEIVER = "com.demo.receiver"; /** * 动态注册广播 */ public void registerMessageReceiver() { mMessageReceiver = new MessageReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(ACTION_INTENT_RECEIVER); registerReceiver(mMessageReceiver, filter); } public class MessageReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if (intent.getAction().equals(ACTION_INTENT_RECEIVER)) { mTextView.setText(intent.getStringExtra("message")); } } } }
二:自定义广播接收器
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; public class MyBroadCast extends BroadcastReceiver{ public MyBroadCast() { Log.v("BROADCAST_TAG", "MyBroadCast"); } @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Log.v("BROADCAST_TAG", "onReceive"); Bundle bundle = intent.getExtras(); if(intent.getAction().equals(MainActivity.ACTION_INTENT_TEST)) { processCustomMessage(context, bundle); } } //send msg to MainActivity private void processCustomMessage(Context context, Bundle bundle) { Intent mIntent=new Intent(MainActivity.ACTION_INTENT_RECEIVER); mIntent.putExtra("message", "测试Broadcast与Activity之间的通信"); context.sendBroadcast(mIntent); } }三:配置注册静态广播
<receiver android:name=".MyBroadCast"> <intent-filter > <action android:name="com.demo.test"/> </intent-filter> </receiver>
0 0
- Android 设置后台静态广播和activity进行通信
- android 广播BroadcastReceiver(activity与service通信)
- Activity和Fragment通信------发送广播Broadcast
- android 功能代码 后台服务通过广播更新Activity界面
- 关于android 下activity间通信的问题-广播机制
- Android编程-IntentService使用广播与Activity通信
- android service与activity进行通信
- activity和service之间如何进行通信?
- 使用EventBus进行Fragment和Activity通信
- 使用EventBus进行Fragment和Activity通信
- Fragment和Activity之间进行通信
- Activity和Fragment如何进行通信
- activity和service之间如何进行通信?
- Activity和Service 使用Binder进行通信
- Android中的静态系统广播和动态系统广播
- Android中的静态系统广播和动态系统广播
- Android中的静态系统广播和动态系统广播
- Android 静态广播和动态广播接收顺序
- ==$0
- 关闭键盘
- 数据结构算法之动态规划、贪心算法
- 关于浏览器兼容的问题以及字符串编码的一个坑,鼠标滚动事件mousewheel和DOMMouseScroll
- 压电蜂鸣器与电磁蜂鸣器
- Android 设置后台静态广播和activity进行通信
- 找邊緣(Laplacian)
- 为TMenuItem增加指针Data属性
- 【Java并发编程实战】—–“J.U.C”:锁,lock
- 通过SecureCRT安装JDK
- 【Linux】添加sudo用户
- scala 中的类定义与getter setter
- eclipse 打包apk时 出现 no dex file found
- 【USACO 2002 February】BUY LOW,BUY LOWER 低价购买