广播(BroadcastReceiver)
1.广播
Android的四大组件之一 广播。在开发中应用比较广泛,对于应用与应用之间,底层与应用之间通常采用这种形式进行数据的传递,实现IPC(进程间的通信)。下面简单的讲解一下广播。
2.广播类型
1.标准广播
标准广播是一种异步执行的广播,各个广播接受器之间不会彼此影响,接受效率较高。
1.系统广播
Android系统内置了较多的广播,手机开关机,电量变化,锁屏等事件都可以通过广播进行监听。我们可以方便的对它们做出反应。广播的注册形式分为两种,一种通过AndroidManifest.xml中注册。另一种,通过java代码的形式进行注册。
-
1.动态注册广播
动态广播主要采用代码的形式进行创建,只能在应用启动后才能注册,相较于静态广播安全可靠。
- 创建广播接收器:
public class ReciverTest extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "耳机状态发生变化", Toast.LENGTH_SHORT).show(); } } }
- 注册广播
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ Button butReceive; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //静态注册广播 init(); } private void onReciver() { registerReceiver(new ReciverTest(), new IntentFilter(Intent.ACTION_HEADSET_PLUG)); } private void init() { butReceive = findViewById(R.id.button_receive1); butReceive.setOnClickListener(this); } @Override public void onClick(View v) { onReciver(); } @Override protected void onDestroy() { unregisterReceiver(reciverTest); super.onDestroy(); } }
registerReceiver()仔细看一下这个方法,这个方法就是用来注册动态广播的核心,第一个参数是一个BroadcastReceiver的广播接收器;第二个参数:IntentFilter类,该类的构造器的参数是字符串。注册后就会筛选出我们需要接受的内容,也就是IntentFilter构造器参数,每当筛选到该广播,就会通过第一个参数对应的广播进行监听。
动态注册总是成对存在的,注册之后必须注销,并且注册与注销的时机要一一对应。在Activity中的注册与注销中如下,对于服务等生命周期中也类似。
注册 onCreate() onStart() onResume() 注销 onDestory() onStop() onPaususe() -
静态注册广播
对于静态广播的注册,只需要在AndroidManifest.xml中注册,然后创建相应的广播接收器就可以了,过程如下:
注意 随着Android系统的安全性不断提高,很多系统广播不能直接静态注册使用。
- 1.创建广播接收器
public class ReciverTest extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); switch(action){ case Intent.ACTION_HEADSET_PLUG: Toast.makeText(context, "耳机状态发生变化", Toast.LENGTH_SHORT).show(); break; case ACTION_BOOT_COMPLETED: Toast.makeText(context, "手机已开机", Toast.LENGTH_SHORT).show(); break; case Intent.ACTION_SHUTDOWN: Toast.makeText(context, "手机正在关机", Toast.LENGTH_SHORT).show(); break; case Intent.ACTION_USER_PRESENT: Toast.makeText(context, "手机正在解锁", Toast.LENGTH_SHORT).show(); break; default: break; } } }
- 2.在AndroidManifest.xml中注册:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.broadcast"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <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.BroadCast"> <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=".reciver.ReciverTest" android:enabled="true" android:exported="true"> <intent-filter> <!-- 开机广播 --> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> <intent-filter> <!-- 关机广播 --> <action android:name="android.intent.action.ACTION_SHUTDOWN" /> </intent-filter> <!-- 解锁广播 --> <intent-filter> <action android:name="android.intent.action.USER_PRESENT" /> </intent-filter> </receiver> </application> </manifest>
对于静态广播,我们仅仅通过在xml文件中注册就可以了,当广播发送后就会找到清单列表中对应的该广播。
2.自定义广播
-
1.自定义广播
- 1.清单中注册;
<receiver android:name=".reciver.ReceiverTestOne" android:exported="true" android:enabled="true"> <intent-filter> <action android:name="com.example.broadcast.MY_BROADCAST"/> </intent-filter> </receiver>
标签中的android:name的属性是自定义的名字,可以任意;
- 2.BroadcastReceiver广播接收类
public class ReceiverTestOne extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, ""+intent.getAction(),Toast.LENGTH_SHORT).show(); } }
- 发送自定义的广播
public void reciverOne(){ //参数:com.example.broadcast.reciver.MY_BROADCAST Intent intent = new Intent("com.example.broadcast.MY_BROADCAST"); //ComponentName() intent.setComponent(new ComponentName("com.example.broadcast","com.example.broadcast.reciver.ReceiverTestOne")); sendBroadcast(intent); }
对于自定义的广播,我们就可以通过动态注册形式进行使用。
广播(BroadcastReceiver)
1.广播
Android的四大组件之一 广播。在开发中应用比较广泛,对于应用与应用之间,底层与应用之间通常采用这种形式进行数据的传递,实现IPC(进程间的通信)。下面简单的讲解一下广播。
2.广播类型
1.标准广播
标准广播是一种异步执行的广播,各个广播接受器之间不会彼此影响,接受效率较高。
1.系统广播
Android系统内置了较多的广播,手机开关机,电量变化,锁屏等事件都可以通过广播进行监听。我们可以方便的对它们做出反应。广播的注册形式分为两种,一种通过AndroidManifest.xml中注册。另一种,通过java代码的形式进行注册。
-
1.动态注册广播
动态广播主要采用代码的形式进行创建,只能在应用启动后才能注册,相较于静态广播安全可靠。
- 创建广播接收器:
public class ReciverTest extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "耳机状态发生变化", Toast.LENGTH_SHORT).show(); } } }
- 注册广播
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ Button butReceive; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //静态注册广播 init(); } private void onReciver() { registerReceiver(new ReciverTest(), new IntentFilter(Intent.ACTION_HEADSET_PLUG)); } private void init() { butReceive = findViewById(R.id.button_receive1); butReceive.setOnClickListener(this); } @Override public void onClick(View v) { onReciver(); } @Override protected void onDestroy() { unregisterReceiver(reciverTest); super.onDestroy(); } }
registerReceiver()仔细看一下这个方法,这个方法就是用来注册动态广播的核心,第一个参数是一个BroadcastReceiver的广播接收器;第二个参数:IntentFilter类,该类的构造器的参数是字符串。注册后就会筛选出我们需要接受的内容,也就是IntentFilter构造器参数,每当筛选到该广播,就会通过第一个参数对应的广播进行监听。
动态注册总是成对存在的,注册之后必须注销,并且注册与注销的时机要一一对应。在Activity中的注册与注销中如下,对于服务等生命周期中也类似。
注册 onCreate() onStart() onResume() 注销 onDestory() onStop() onPaususe() -
静态注册广播
对于静态广播的注册,只需要在AndroidManifest.xml中注册,然后创建相应的广播接收器就可以了,过程如下:
注意 随着Android系统的安全性不断提高,很多系统广播不能直接静态注册使用。
- 1.创建广播接收器
public class ReciverTest extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); switch(action){ case Intent.ACTION_HEADSET_PLUG: Toast.makeText(context, "耳机状态发生变化", Toast.LENGTH_SHORT).show(); break; case ACTION_BOOT_COMPLETED: Toast.makeText(context, "手机已开机", Toast.LENGTH_SHORT).show(); break; case Intent.ACTION_SHUTDOWN: Toast.makeText(context, "手机正在关机", Toast.LENGTH_SHORT).show(); break; case Intent.ACTION_USER_PRESENT: Toast.makeText(context, "手机正在解锁", Toast.LENGTH_SHORT).show(); break; default: break; } } }
- 2.在AndroidManifest.xml中注册:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.broadcast"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <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.BroadCast"> <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=".reciver.ReciverTest" android:enabled="true" android:exported="true"> <intent-filter> <!-- 开机广播 --> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> <intent-filter> <!-- 关机广播 --> <action android:name="android.intent.action.ACTION_SHUTDOWN" /> </intent-filter> <!-- 解锁广播 --> <intent-filter> <action android:name="android.intent.action.USER_PRESENT" /> </intent-filter> </receiver> </application> </manifest>
对于静态广播,我们仅仅通过在xml文件中注册就可以了,当广播发送后就会找到清单列表中对应的该广播。
2.自定义广播
-
1.自定义广播
- 1.清单中注册;
<receiver android:name=".reciver.ReceiverTestOne" android:exported="true" android:enabled="true"> <intent-filter> <action android:name="com.example.broadcast.MY_BROADCAST"/> </intent-filter> </receiver>
标签中的android:name的属性是自定义的名字,可以任意;
- 2.BroadcastReceiver广播接收类
public class ReceiverTestOne extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, ""+intent.getAction(),Toast.LENGTH_SHORT).show(); } }
- 发送自定义的广播
public void reciverOne(){ //参数:com.example.broadcast.reciver.MY_BROADCAST Intent intent = new Intent("com.example.broadcast.MY_BROADCAST"); //ComponentName() intent.setComponent(new ComponentName("com.example.broadcast","com.example.broadcast.reciver.ReceiverTestOne")); sendBroadcast(intent); }
对于自定义的广播,我们就可以通过动态注册形式进行使用。