压缩包

test2.zip

一、广播:分为系统广播,与用户自定义广播

二、静态注册接收广播

1.创建广播接收者的类继承广播

image.png

2.在清单文件注册该广播类

image.png

3.创建一个接口,定义静态注册和动态注册的变量

image.png

4.创建一个Main4Activity活动,定义布局,实现点击之后发送广播

image.png

三、静态注册接收广播的整体代码

1.布局activity_main4.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".Main4Activity"
  8. android:orientation="vertical">
  9. <TextView
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="动态注册广播"
  13. android:textSize="30dp"
  14. />
  15. <Button
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="发送广播1"
  19. android:textSize="30sp"
  20. android:onClick="sendAction1"
  21. />
  22. <TextView
  23. android:layout_marginTop="100dp"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="静态注册广播"
  27. android:textSize="30sp"
  28. />
  29. <Button
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:text="发送广播2"
  33. android:textSize="30dp"
  34. android:onClick="sendAction2"
  35. />
  36. </LinearLayout>

2.清单AndroidManifest.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.test">
  4. <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:roundIcon="@mipmap/ic_launcher_round"
  9. android:supportsRtl="true"
  10. android:theme="@style/AppTheme">
  11. <activity android:name=".Main3Activity"></activity>
  12. <!-- 组件比须在清单文件里面注册 -->
  13. <activity android:name=".MActivity" />
  14. <!-- 默认的Activity -->
  15. <activity android:name=".Main4Activity">
  16. <!-- 激活默认的Activity的意图 -->
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity> <!-- 组件必须在清单文件里面注册 -->
  22. <service android:name=".MyService" />
  23. <!-- 1.静态注册广播接收者 -->
  24. <receiver android:name=".CustomReceiver">
  25. <!-- 标记 -->
  26. <intent-filter>
  27. <action android:name="com.derry.receiver_flag_" />
  28. </intent-filter>
  29. </receiver>
  30. </application>
  31. </manifest>

3.CustomReceiver文件代码

  1. package com.example.test;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.util.Log;
  6. //广播接收者
  7. public class CustomReceiver extends BroadcastReceiver {
  8. private static final String TAG=CustomReceiver.class.getSimpleName();
  9. @Override
  10. public void onReceive(Context context, Intent intent) {
  11. Log.e(TAG,"CustomReceiver onReceiver 广播接收者");
  12. }
  13. }

4.ActionUtils文件代码

  1. package com.example.test;
  2. public interface ActionUtils {
  3. //广播注册时 与 发送广播时 的 唯一标识,必须要保持一致(给动态注册用)
  4. String ACTION_EQUES_UPDATE_IP="com.derry.receiver_study_";
  5. //广播注册时 与 发送广播时 的 唯一标识,必须要保持一致(给静态注册用)
  6. String ACTION_FLAG="com.derry.receiver_flag_";
  7. }

5.Main4Activity文件代码

  1. package com.example.test;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. public class Main4Activity extends AppCompatActivity {
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main4);
  11. }
  12. public void sendAction1(View view) {
  13. }
  14. // 第二步:发送给接收者
  15. //静态发送广播 给 接收者
  16. public void sendAction2(View view) {
  17. Intent intent = new Intent();
  18. // ACTION_FLAG 与注册时保持一致
  19. intent.setPackage("com.example.test");
  20. intent.setAction(ActionUtils.ACTION_FLAG);
  21. sendBroadcast(intent);
  22. }
  23. }

6.效果图:

image.png

四、动态注册接收广播

1.创建广播接收者的类继承广播

image.png

2.动态发送广播

image.png

五、动态注册接收广播的整体代码

1.UpdateIpSelectCity文件代码

  1. package com.example.test;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.util.Log;
  6. // 第一步,定义广播接收者(Reseiver)
  7. public class UpdateIpSelectCity extends BroadcastReceiver {
  8. private static final String TAG= UpdateIpSelectCity.class.getSimpleName();
  9. @Override
  10. public void onReceive(Context context, Intent intent) {
  11. Log.e(TAG,"UpdateIpSelectCity onReceiver 广播接收者");
  12. }
  13. }

2.Main4Activity文件代码

  1. package com.example.test;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.content.IntentFilter;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. public class Main4Activity extends AppCompatActivity {
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main4);
  12. // Java 代码 来注册 刚刚的接收者即可
  13. // 第二步,在onCreate注册广播(订阅)
  14. // 动态使用Java代码注册一个广播接收者
  15. UpdateIpSelectCity updateIpSelectCity = new UpdateIpSelectCity();
  16. //固定器
  17. IntentFilter intentFilter = new IntentFilter();
  18. intentFilter.addAction(ActionUtils.ACTION_EQUES_UPDATE_IP);
  19. registerReceiver(updateIpSelectCity,intentFilter);
  20. }
  21. // 第三步
  22. // 发送给 动态注册的接收者
  23. public void sendAction1(View view) {
  24. Intent intent = new Intent();
  25. // ACTION_FLAG 与注册时保持一致
  26. intent.setPackage("com.example.test");
  27. intent.setAction(ActionUtils.ACTION_EQUES_UPDATE_IP);
  28. sendBroadcast(intent);
  29. }
  30. // 第二步:发送给接收者
  31. //静态发送广播 给 接收者
  32. public void sendAction2(View view) {
  33. Intent intent = new Intent();
  34. // ACTION_FLAG 与注册时保持一致
  35. intent.setPackage("com.example.test");
  36. intent.setAction(ActionUtils.ACTION_FLAG);
  37. sendBroadcast(intent);
  38. }
  39. }

3.效果图:

image.png