1.什么是广播
2.广播的分类及区别——有序广播和无序广播
3.广播的注册方式及区别——静态注册和动态注册
4.静态注册无序广播
5.静态注册有序广播
6.动态注册无序广播
7.动态注册有序广播
8.适配器中发送广播实例
9.Service中发送广播实例
什么是广播
1.广播是Android四大组件之一,全称BroadcastReceiver
2.广播分为发送方和接收方,就算没有接收方,发送方也会发送数据
3.广播可以实现跨多个页面数据通信
4.广播可以实现跨进程间的数据通信
有序广播和无序广播
- 无序广播
不管有多少接收者,一旦发出了无序广播,所有监听此广播的接收者都能接收到 - 有序广播
按照接收者的优先级,逐一传递,优先级最高的接收者先接收,如果不终止此广播再往下传递
静态注册发送无序广播
1.新建类继承BroadcastReceiver
2.实现onReceive抽象方法。
3.在AndroidManifest中注册BroadReceiver
静态注册有序广播
package com.example.myapplication.broadcast;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.myapplication.R;
public class BroadcastActivity extends AppCompatActivity implements View.OnClickListener {
private Button broadcast_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_broadcast);
broadcast_btn = findViewById(R.id.broadcast_btn);
broadcast_btn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.broadcast_btn:
Intent intent=new Intent();
intent.setAction("Intercept_Stitch");
Bundle bundle=new Bundle();
bundle.putString("name","叶楚楚");
intent.putExtras(bundle);
sendOrderedBroadcast(intent,null);
break;
default:
break;
}
}
}
动态注册无序广播
1.新建TestBroadcast类继承BroadcastReceiver。
2.实现onReceive抽象方法,由于BroadcastReceiver是一个抽象类,定义了一个onReceive抽象方法,所以必须实现此抽象方法。
3.在代码中实例化TestBroadcast广播类
4.在代码中实例化IntentFilter。
5.调用IntentFilter对象addAction方法设置频道号
6.在有context环境中调用registerReceiver注册广播
7.在有context环境中通过sendBroadcast发送广播
8.在有context环境中通过unregisterReceiver注销广播
动态注册有序广播
package com.example.myapplication.broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private static final String MY_BROADCAST_ACTION = "com.example.mybroadcastreceiver.my_broadcast_action";
private FirstBroadCast firstBroadCast;
private TwiceBroadCast twiceBroadCast;
private ThirdBroadCast thirdBroadCast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstBroadCast = new FirstBroadCast();
IntentFilter filter1 = new IntentFilter();
filter1.addAction(MY_BROADCAST_ACTION);
filter1.setPriority(1000);
registerReceiver(firstBroadCast, filter1);
twiceBroadCast = new TwiceBroadCast();
IntentFilter filter2 = new IntentFilter();
filter2.addAction(MY_BROADCAST_ACTION);
filter1.setPriority(5);
registerReceiver(twiceBroadCast, filter2);
thirdBroadCast = new ThirdBroadCast();
IntentFilter filter3 = new IntentFilter();
filter3.addAction(MY_BROADCAST_ACTION);
filter1.setPriority(6);
registerReceiver(thirdBroadCast, filter3);
// finish();
}
public void sendBroadCast(View v){
//这里创建电台来发送广播,需要一个intent就行了
Intent intent = new Intent();
//给广播设置一个action,方便广播接收者接收相应的广播
intent.setAction(MY_BROADCAST_ACTION);
intent.putExtra("data", "这里是来自自定义电台发送的广播");
// sendBroadcast(intent);//这里发送的是无序广播
sendOrderedBroadcast(intent, null, null, null, 1, "10000", null);
}
registerReceiver方法注册
class FirstBroadCast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (MY_BROADCAST_ACTION.equals(action)) {
String data = intent.getStringExtra("data");
Toast.makeText(context,
"FirstBroadCast接收到广播了,接收的广播的数据是:" + data, 1).show();
System.out.println("FirstBroadCast接收到广播了,接收的广播的数据是:" + data);
}
}
}
class TwiceBroadCast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (MY_BROADCAST_ACTION.equals(action)) {
String data = intent.getStringExtra("data");
Toast.makeText(context,
"TwiceBroadCast接收到广播了,接收的广播的数据是:" + data, 1).show();
System.out.println("TwiceBroadCast接收到广播了,接收的广播的数据是:" + data);
}
}
}
class ThirdBroadCast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (MY_BROADCAST_ACTION.equals(action)) {
String data = intent.getStringExtra("data");
Toast.makeText(context,
"ThirdBroadCast接收到广播了,接收的广播的数据是:" + data, 1).show();
System.out.println("ThirdBroadCast接收到广播了,接收的广播的数据是:" + data);
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(firstBroadCast != null){
unregisterReceiver(firstBroadCast);
}
if(twiceBroadCast != null){
unregisterReceiver(twiceBroadCast);
}
if(thirdBroadCast != null){
unregisterReceiver(thirdBroadCast);
}
}
}
Service中发送广播实例
package com.example.myapplication.broadcast;
import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class ImageBroadcast extends Service{
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url=new URL("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529499172843&di=1bd010d32a1b039f9e1836fd4fc86b48&imgtype=jpg&src=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D1950000289%2C454533439%26fm%3D214%26gp%3D0.jpg");
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
InputStream inputStream=connection.getInputStream();
Bitmap bitmap= BitmapFactory.decodeStream(inputStream);
Log.e("3213112342", "run: "+bitmap);
Intent intent=new Intent();
intent.setAction("com.example.myapplication.img");
intent.putExtra("bitmap",bitmap);
sendBroadcast(intent);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
package com.example.myapplication.broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.example.myapplication.R;
public class ImageBroadcastActivity extends AppCompatActivity {
private ImageView img_broadcast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_broadcast);
MyReceiver1 myReceiver1=new MyReceiver1();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("com.example.myapplication.img");
registerReceiver(myReceiver1,intentFilter);
img_broadcast=findViewById(R.id.img_broadcast);
Intent intent = new Intent(ImageBroadcastActivity.this, ImageBroadcast.class);
startService(intent);//启动service
}
class MyReceiver1 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Bitmap bitmap=intent.getParcelableExtra("bitmap");
img_broadcast.setImageBitmap(bitmap);
}
}
}