广播的思维导图:
广播即一个发送者向多个接收者发送消息,以下为大家展示一下发送者与接收者的代码示例:
发送者xml部分:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.zking.administrator.g160628_android21_sender.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_main_content"
android:hint="请输入要发送的内容"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送"
android:onClick="send"
/>
</LinearLayout>
java部分:
package com.zking.administrator.g160628_android21_sender;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText et_main_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_main_content = (EditText) findViewById(R.id.et_main_content);
}
public void send(View view){
String content=et_main_content.getText().toString();
//发送广播
//两种类型广播
//有序广播 无序广播
Intent intent=new Intent();
//指定广播的名字
intent.setAction("com.zking.administrator.g160628_android21_sender.Hug");
//指定广播内容
intent.putExtra("content",content);
//发送无序广播
//sendBroadcast(intent);
//发送无序黏性广播
//sendStickyBroadcast(intent);
//发送有序广播
sendOrderedBroadcast(intent,null);
}
}
接受者java部分:
自创类MyReceiver01:
package com.zking.administrator.g160628_android21_receiver01;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
/**
* Created by Administrator on 2017/7/11 0011.
*/
public class MyReceiver01 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获取广播的名字
String action=intent.getAction();
if("com.zking.administrator.g160628_android21_sender.Hug".equals(action)){
//获取广播内容
//String content=intent.getStringExtra("content");
//获取结果
Bundle bundle=getResultExtras(true);
String content=bundle.getString("newContent");
Log.i("test","广播接受者1号:"+content);
}
}
}
MainActivity:
package com.zking.administrator.g160628_android21_receiver01;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private MyReceiver01 myReceiver01;
private IntentFilter intentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myReceiver01 = new MyReceiver01();
intentFilter = new IntentFilter();
intentFilter.addAction("com.zking.administrator.g160628_android21_sender.Hug");
intentFilter.setPriority(998);
}
@Override
protected void onResume() {
super.onResume();
//注册广播
registerReceiver(myReceiver01,intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
//取消注册 注销
unregisterReceiver(myReceiver01);
}
}