1、广播
广播包括发广播和广播接收者
1.1、监听电量变化的广播
public class MainActivity extends AppCompatActivity {
private TextView mBatteryLevel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
registerBatteryReceiver();
}
private void initView() {
mBatteryLevel = this.findViewById(R.id.battery_level);
}
private void registerBatteryReceiver() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
BatteryStatusReceiver receiver = new BatteryStatusReceiver();
this.registerReceiver(receiver,intentFilter);
}
public class BatteryStatusReceiver extends BroadcastReceiver {
private static final String TAG = "BatteryStatusReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "action is == " + action);
if(mBatteryLevel != null){
mBatteryLevel.setText("当前电量:" + intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0));
}
int maxLevel = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
float percent = currentLevel * 1.0f / maxLevel * 100;
Log.d(TAG, "当前电量比为:" + percent + "%");
}
}
}
注意权限:
1.2、监听USB线拔出和插入
intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
public class BatteryStatusReceiver extends BroadcastReceiver {
private static final String TAG = "BatteryStatusReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
Log.d(TAG, "action is == " + action);
if(mBatteryLevel != null){
mBatteryLevel.setText("当前电量:" + intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0));
}
int maxLevel = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
float percent = currentLevel * 1.0f / maxLevel * 100;
Log.d(TAG, "当前电量比为:" + percent + "%");
} else if(Intent.ACTION_POWER_CONNECTED.equals(action)){
Log.d(TAG, "usb连接");
} else if(Intent.ACTION_POWER_DISCONNECTED.equals(action)){
Log.d(TAG, "usb断开");
}
}
}
1.3、取消注册
private void registerBatteryReceiver() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
mReceiver = new BatteryStatusReceiver();
this.registerReceiver(mReceiver,intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mReceiver != null) {
this.unregisterReceiver(mReceiver);
}
}
1.4、静态注册监听开机广播
这里推荐使用动态注册。
从 Android 8.0(API 级别 26)开始,系统对清单声明的接收器施加了额外的限制。 如果您的应用以 Android 8.0 或更高版本为目标平台,那么对于大多数隐式广播(没有明确针对您的应用的广播),您不能使用清单来声明接收器。当用户正在活跃地使用您的应用时,您仍可使用上下文注册的接收器。
创建广播接收者
public class BootCompleteRecevicer extends BroadcastReceiver {
private static final String TAG = "BootCompleteRecevicer";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "action " + action);
Log.d(TAG, "开机完成");
Toast.makeText(context,"收到开机完成的广播", Toast.LENGTH_SHORT).show();
}
}
<receiver android:name=".BootCompleteRecevicer">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
权限
1.5、监听应用于安装
监听应用的安装与卸载目的是为了收集信息。
<receiver android:name=".AppStateChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
public class AppStateChangeReceiver extends BroadcastReceiver {
private static final String TAG = "AppStateChangeReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_PACKAGE_ADDED)) {
Log.d(TAG, "应用安装了,信息是 " + intent.getData());
}else if(action.equals(Intent.ACTION_PACKAGE_REMOVED)){
Log.d(TAG, "应用卸载了,信息是 " + intent.getData());
}
}
}
1.6、自定义广播和接收
创建广播发送者
public class SendBroadcastActivity extends Activity {
private EditText mInputBox;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
initView();
}
private void initView() {
mInputBox = this.findViewById(R.id.msg_et);
}
public void sendBroadcastMsg(View view){
String inputContent = mInputBox.getText().toString();
Intent intent = new Intent();
intent.setAction(Constans.ACTION_SEND_MSG);
intent.putExtra(Constans.KEY_CONTENT, inputContent);
sendBroadcast(intent);
}
}
创建广播接收者
public class MessageReceiver extends BroadcastReceiver {
private static final String TAG = "MessageReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "action is " + action);
String content = intent.getStringExtra(Constans.KEY_CONTENT);
Log.d(TAG, "content is " + content);
}
}
注册
<activity android:name=".SendBroadcastActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MessageReceiver">
<intent-filter>
<action android:name="com.example.broadcastdemo.SENGD_MSG"/>
</intent-filter>
</receiver>
1.7、广播接收者之有序广播的发送
不是重点
<receiver android:name=".SchoolReceiver">
<intent-filter android:priority="1000">
<action android:name="com.sunofbeaches.broadcastdemo.DONATION"/>
</intent-filter>
</receiver>
<receiver android:name=".GradeReceiver">
<intent-filter android:priority="500">
<action android:name="com.sunofbeaches.broadcastdemo.DONATION"/>
</intent-filter>
</receiver>
<receiver android:name=".ClassReceiver">
<intent-filter android:priority="100">
<action android:name="com.sunofbeaches.broadcastdemo.DONATION"/>
</intent-filter>
</receiver>
<receiver android:name=".PoorStudentReceiver">
<intent-filter android:priority="-1000">
<action android:name="com.sunofbeaches.broadcastdemo.DONATION"/>
</intent-filter>
</receiver>
1.8、有序广播的终止广播以及修改广播内容
1.8.1、终止广播
1.8.2、修改广播内容
高优先级的才能修改
作者笔记
1.9、广播权限
1.9.1、谁有权限接收
操作:
-
发送广播的地方去声明权限。(包名+权限名) -
发送广播的时候带上权限。 -
接收端要带上权限。否则接收不到
1.9.2、谁有权限发我
指定谁有权限可以发给我
接收端:
发送端:
|