什么是广播?
Android的四大组件之一,是一种广泛应用在应用程序之间传输信息的机制。
静态注册无序广播,动态注册无序广播,静态注册有序广播,动态注册有序广播。
第一个工程:
package com.example.abc.weixidemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.weixi_btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//发送广播
Intent intent = new Intent("zhifubao");
sendBroadcast(intent);//无序发送
//sendOrderedBroadcast(intent,null);//有序发送
}
});
}
}
第二个工程:
在manifest中接收广播的几种情况。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.abc.zhifubaodemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="支付宝"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--优先级priority的取值从-1000到1000-->
<!-- <receiver android:name=".MyBroad"> //有序静态注册
<intent-filter android:priority="1000"> //优先级(没有这个就是无序————变成只有intent-filter)
<action android:name="zhifubao"></action> //名字
</intent-filter>
</receiver>
<receiver android:name=".MyBroad2">
<intent-filter android:priority="-1000">
<action android:name="zhifubao"></action>
</intent-filter>
</receiver>-->
</application>
</manifest>
新建Class类继承BroadcastReceiver
package com.example.abc.zhifubaodemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.widget.Toast;
/**
* Created by Administrator on 2018/3/26.
*/
public class MyBroad extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "......", Toast.LENGTH_SHORT).show();//显示结果
Log.e("abc", "onReceive: ............................");
//abortBroadcast();//静态注册有序结束用语————不用会出错
}
}
如果是动态注册在MainActivity用java代码进行注册。
package com.example.abc.zhifubaodemo;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private MyBroad broadcast;
private MyBroad2 broadcast1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
broadcast= new MyBroad();//第一个广播(有序动态注册)
IntentFilter filter = new IntentFilter("zhifubao");
//里面放由weixidemo传来的暗号
filter.setPriority(-1000);//优先级(没有就是无序动态注册)
registerReceiver(broadcast, filter);
broadcast1= new MyBroad2();//另一种写法
IntentFilter filter1 = new IntentFilter();
filter1.addAction="zhifubao";
filter.setPriority(1000);
registerReceiver(broadcast1, filter1);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(broadcast);//动态注册有序——结束广播
unregisterReceiver(broadcast1);//没有退出会出错
}
}