今天跟着老师学习了BroadcastReceiver,来记录一下。
发送标准广播(采用静态注册,即手动在AndroidManifest.xml里写注册代码)
看一下我的项目结构
MainActivity代码如下
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private final String NORMAL_ACTION = "com.example.broadcastreceiver.MyReceiver";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//发送一条广播
Intent intent = new Intent(NORMAL_ACTION);
intent.putExtra("Msg", "Hi");
intent.setComponent(new ComponentName("com.example.broadcastreceiver", "com.example.broadcastreceiver.MyReceiver"));
sendBroadcast(intent); //发送的是标准广播
}
});
}
}
MainActivity布局文件activity_main.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"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/btn"
android:text="发送标准广播"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MyReceiver代码如下
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("Msg");
Log.e("TAG", msg);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceiver">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication1">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--注册广播接收器-->
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="com.example.broadcastreceiver.MyReceiver" />
</intent-filter>
</receiver>
</application>
</manifest>
运行结果(我发送了多条广播)
到此发送标准广播的代码粘贴完了,其中每行代码都很容易理解,我就不一一解释了(也没写多少行代码),但是我还是要特别讲一下MainActivity里得这行代码
intent.setComponent(new ComponentName("com.example.broadcastreceiver", "com.example.broadcastreceiver.MyReceiver"));
为什么要加这行代码呢?刚开始学时,没加这行代码,一运行发现,MyReceiver接收不到MainActivity发送的广播,于是我就上网查了一下https://developer.android.google.cn/about/versions/oreo/background
大概意思是android8.0及以上系统关于广播的规定:当targetSdkVersion >=26时,在AndroidManifest.xml里面注册的Receiver可能无法接收到广播消息,需要加上上面那行代码,该行代码第一个参数为包的路径,第二个参数为类名。至于这行代码是啥意思我就不太清楚了,如果想知道的可以去查看源码。
发送有序广播(还是采用静态注册)
看一下我的项目结构
MainActivity代码如下
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private final String ORDER_ACTION = "com.example.broadcastreceiver2.MyReceiver";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ORDER_ACTION);
intent.putExtra("Msg", "奥力给");
Log.i("MainActivity", "onClick: MainActivity发送的消息是:奥力给");
intent.setPackage(getPackageName());
//intent.setComponent(new ComponentName("com.example.broadcastreceiver2", "com.example.broadcastreceiver2.MyReceiver1"));
//intent.setComponent(new ComponentName("com.example.broadcastreceiver2", "com.example.broadcastreceiver2.MyReceiver2"));
//intent.setComponent(new ComponentName("com.example.broadcastreceiver2", "com.example.broadcastreceiver2.MyReceiver3"));
sendOrderedBroadcast(intent, null);
//发送有序广播调用的是 sendOrderedBroadcast(Intent , String) 方法,String 参数值在自定义权限时使用
}
});
}
}
MainActivity的布局文件activity_main.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"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/btn"
android:text="发送有序广播"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MyReceiver1代码如下
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class MyReceiver1 extends BroadcastReceiver {
private String TAG="Receiver1";
@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "MyReceiver1被调用了");
//取出Intent当中传递来的数据
String msg = intent.getStringExtra("Msg");
Log.e(TAG, "MyReceiver1接收到的值是: " + msg);
//向下一优先级的Receiver传递数据
Bundle bundle = new Bundle();
bundle.putString("Data", "fating");
Log.i(TAG, "MyReceiver1发送的消息是:fating");
setResultExtras(bundle);
}
}
MyReceiver2代码如下
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class MyReceiver2 extends BroadcastReceiver {
private String TAG="Receiver2";
@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "MyReceiver2被调用了");
//取出上一优先级的Receiver传递来的数据
String data = getResultExtras(true).getString("Data");
Log.e(TAG, "MyReceiver2接收到的值是: " + data);
//向下一优先级的Receiver传递数据
Bundle bundle = new Bundle();
bundle.putString("Data", "加油");
Log.i(TAG, "MyReceiver2发送的消息是:加油");
setResultExtras(bundle);
}
}
MyReceiver3代码如下
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver3 extends BroadcastReceiver {
private String TAG="Receiver3";
@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "MyReceiver3被调用了");
//取出上一优先级的Receiver传递来的数据
String data = getResultExtras(true).getString("Data");
Log.e(TAG, "MyReceiver3接收到的值是: " + data);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceiver2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication1">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--注册广播接收器-->
<receiver android:name=".MyReceiver1">
<intent-filter android:priority="1000">
<action android:name="com.example.broadcastreceiver2.MyReceiver" />
</intent-filter>
</receiver>
<receiver android:name=".MyReceiver2">
<intent-filter android:priority="900">
<action android:name="com.example.broadcastreceiver2.MyReceiver" />
</intent-filter>
</receiver>
<receiver android:name=".MyReceiver3">
<intent-filter android:priority="800">
<action android:name="com.example.broadcastreceiver2.MyReceiver" />
</intent-filter>
</receiver>
</application>
</manifest>
运行结果
到此发送有序广播的代码就粘贴完了,如果第一个发送标准广播的你看懂了,发送有序广播的你也一定能看懂,这里遇到的问题是,我发现再在MainActivity里加这行代码是没用的(不用我讲也知道)
intent.setComponent(new ComponentName("com.example.broadcastreceiver", "com.example.broadcastreceiver.MyReceiver"));
因为该行代码的第二个参数为类名,我这里有三个接收广播的类,所以就难搞了,于是我又去上网查找大佬们的博客(不懂就上网查),查了许久,发现在MainActivity里加这行代码可行
intent.setPackage(getPackageName());
这行代码意思就是指定广播接收者的包名,即发送显式广播
动态注册(其实就是用Java代码注册,这里在 Service 中注册广播和接收广播)
看一下我的项目结构
MainActivity代码如下
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
public final static String ACTION = "com.example.broadcastreceiver3";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_startservice = findViewById(R.id.btn_startservice);
Button btn_sendbroadcast = findViewById(R.id.btn_sendbroadcast);
Button btn_stopservice = findViewById(R.id.btn_stopservice);
btn_startservice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, BroadcastService.class);
startService(intent);
}
});
btn_sendbroadcast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ACTION);
sendBroadcast(intent);
}
});
btn_stopservice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, BroadcastService.class);
stopService(intent);
}
});
}
}
MainActivity的布局文件activity_main.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"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/btn_startservice"
android:layout_width="290dp"
android:layout_height="wrap_content"
android:text="开启服务" />
<Button
android:id="@+id/btn_sendbroadcast"
android:layout_width="290dp"
android:layout_height="wrap_content"
android:text="发送广播"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/btn_stopservice"
android:layout_width="290dp"
android:layout_height="wrap_content"
android:text="停止服务"
android:layout_marginTop="10dp"/>
</LinearLayout>
BroadcastService代码如下
package com.example.broadcastreceiver3;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
public class BroadcastService extends Service {
private BroadcastReceiver receiver;
private final String TAG = "BroadcastService";
public BroadcastService() {
}
@Override
public void onCreate() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MainActivity.ACTION);
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "BroadcastService接收到了广播");
}
};
registerReceiver(receiver, intentFilter);
Log.e(TAG, "BroadcastService注册了接收器");
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
Log.e(TAG, "BroadcastService取消注册接收器");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
AndroidManifest.xml(这里只需要注册Service,不需要注册Receiver)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceiver3">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication1">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".BroadcastService"
android:enabled="true"
android:exported="true">
</service>
</application>
</manifest>
运行结果
Context.registerReceiver() 方法是注册接收器
Context.unregisterReceiver() 方法是取消注册
到此对BroadcastReceiver的介绍就结束了,介绍的都很浅,还得继续努力。