Android广播实现进程间通信,很简单
Posted zhangjin1120
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android广播实现进程间通信,很简单相关的知识,希望对你有一定的参考价值。
应用A发送广播:
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public void sendBro(View view)
Intent intent = new Intent("广播A");
intent.putExtra("name","小明");
sendBroadcast(intent);
应用B接收广播:
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final IntentFilter filter = new IntentFilter();
filter.addAction("广播A");
BroadcastReceiver mAReceiver = new BroadcastReceiver()
@Override
public void onReceive(final Context context, final Intent intent)
String action = intent.getAction();
String msg = intent.getStringExtra("name");
Log.d("xxx", "应用B收到:" + action + ",name:" + msg);
;
registerReceiver(mAReceiver, filter);
应用A、B 都安装,发送两个广播,看看log:
就是这么简单!
以上是关于Android广播实现进程间通信,很简单的主要内容,如果未能解决你的问题,请参考以下文章