1.什么是广播
broadcast广播接收器
是Android四大组件之一,分发送方和接收方,没有接收方;跨多个页面的数据通信
;跨进程通信(不同的app)
2.广播的分类及区别—有序广播·和·无序广播
有序广播:所有监听者都能接收到
无序广播:按照接受着的优先级,逐一传递,且传递着可以终止和改变广播
粘滞广播:如果没有接收者会一直等待被接收
3.广播的注册方法及区别,静态注册和动态注册
静态注册
4.静态注册无序广播
5.静态注册有序广播:在mannifest中加入
<receiver android:name=".BroadCast1">
<intent-filter android:priority="100">
<action android:name="fam106.9">
</action
</intent-filter>
</receiver>
6.动态注册无序广播
<receiver android:name=".BroadCast1">
<intent-filter >
<action android:name="fam106.9">
</action>
</intent-filter>
</receiver>
7.动态注册有序广播
在activity中写入下面代码
broadCast1=new BroadCast1();
IntentFilter intentFilter=new IntentFilter();
//注意发送端和接收端的频道要一样,不然会接收不到
intentFilter.addAction("com.lenovo.fam106.9");
registerReceiver(broadCast1,intentFilter);
哪里要用到就在那里接收用intent接收,
Intent intent=new Intent();
intent.setAction("com.lenovo.fam106.9");
intent.putExtra("name","lily");
sendBroadcast(intent);
8.适配器中发送广播示例
9.service中发送广播示例
在请求网络中,将请求的图片,作为广播直接发送出去
@Override//执行任务
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(new Runnable() {
@Override
public void run() {
getWeb1();
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
private StringBuffer getWeb1() {
try {
//创建url地址对象
URL url1 = new URL("https://ps.ssl.qhimg.com/sdmt/189_135_100/t01b9f849c123e0d845.jpg");
//创建connection对象
HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
//创建输入流对象
InputStream inputStream = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
/*bitmap.compress(Bitmap.CompressFormat.PNG,100,os);
byte[] pic=os.toByteArray();*/
//将接收到的bitmap放入intent中用广播的方式传出去
Intent intent=new Intent("con.le.fam76.9");
intent.putExtra("pic",bitmap);
sendBroadcast(intent);
/*Message message=handler.obtainMessage();
message.obj=bitmap;
handler.sendMessage(message);
*/
} catch (MalformedURLException e2) {
e2.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
return stringBuffer;
}
}
package com.example.xiaozhen.webapplication;
public class MyServiceActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView image_service;
private Button button;
private MyReciver myReciver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_service);
init();
initbroadCast();
}
private void init() {
image_service = (ImageView) findViewById(R.id.image_service);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:
Intent intent = new Intent(MyServiceActivity.this, MyService.class);
startService(intent);
break;
}
}
private void initbroadCast() {
myReciver=new MyReciver();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("con.le.fam76.9");
registerReceiver(myReciver,intentFilter);
}
//自己写一个内部类,继承广播
class MyReciver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
/*byte[] pic = intent.getByteArrayExtra("pic");
Bitmap btp = BitmapFactory.decodeByteArray(pic, 0, pic.length);*/
//用getParcelableExtra方法直接得到bitmap类型的图片
Bitmap btp=intent.getParcelableExtra("pic");
image_service.setImageBitmap(btp);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myReciver);
}
}
10。常用的系统广播及其用法
常用的广播有来电广播,电量广播
ACTION_BATTERY_CHANGED:电池电量改变。
ACTION_BATTERY_LOW:电池电量低。
ACTION_POWER_CONNECTED:系统连接电源。
ACTION_POWER_DISCONNECTED:系统与电源断开。
ACTION_SHUTDOWN:系统被关闭。