背景

通过广播的方式,让指定App代替当前App执行某些功能

无页面App

App需要有一个Activity来激活,可以不添加view

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.emao.uni.zng.ext">
  4. <application
  5. android:icon="@mipmap/ic_launcher"
  6. android:label="@string/app_name"
  7. android:roundIcon="@mipmap/ic_launcher_round"
  8. android:theme="@android:style/Theme.NoDisplay" >
  9. <activity android:name=".MainActivity"
  10. android:exported="true">
  11. <intent-filter>
  12. <action android:name="android.intent.action.MAIN" />
  13. <category android:name="android.intent.category.LAUNCHER" />
  14. </intent-filter>
  15. </activity>
  16. <receiver
  17. android:name="com.emao.uni.zng.receiver.EmaoApkReceiver"
  18. android:exported="true"
  19. android:process=":remote" >
  20. <intent-filter>
  21. <action android:name="android.intent.action.BOOT_COMPLETED" />
  22. <action android:name="com.emao.uni.zng.action.ACTION_INSTALL_APK" />
  23. <action android:name="com.emao.uni.zng.action.ACTION_UNINSTALL_APK" />
  24. <category android:name="android.intent.category.DEFAULT"/>
  25. </intent-filter>
  26. </receiver>
  27. </application>
  28. </manifest>
  1. package com.emao.uni.zng.ext;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. /**
  5. * 空 Activity, 激活App
  6. */
  7. public class MainActivity extends AppCompatActivity {
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. finish();
  12. }
  13. }
  1. package com.emao.uni.zng.receiver;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.util.Log;
  6. import com.emao.uni.zng.ext.MainActivity;
  7. public class MyReceiver extends BroadcastReceiver {
  8. public void onReceive(Context context, Intent intent) {
  9. Log.i("TAG", "intent:" + intent);
  10. // 启动 app, 开机自启动
  11. if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
  12. Intent intent1 = new Intent(context, MainActivity.class);
  13. intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  14. context.startActivity(intent1);
  15. return;
  16. }
  17. if (mService == null) {
  18. mService = new MyService(context);
  19. }
  20. if (ACTION_1.equals(intent.getAction())) {
  21. String param1 = intent.getStringExtra("param1");
  22. String param2 = intent.getStringExtra("param2");
  23. boolean param3 = intent.getBooleanExtra("param1", false);
  24. Log.e("TAG", "param1:" + param1);
  25. Log.e("TAG", "param2:" + param2);
  26. Log.e("TAG", "param3:" + param3);
  27. return;
  28. }
  29. if (ACTION_2.equals(intent.getAction())) {
  30. String param1 = intent.getStringExtra("param1");
  31. Log.e("TAG", "param1:" + param1);
  32. return;
  33. }
  34. }
  35. }

主App

  1. Intent intent1 = new Intent(ACTION_INSTALL_APK);
  2. intent1.setPackage("目标包名");
  3. intent1.putExtra("param1", "/apkPath");
  4. intent1.putExtra("param2", "com.emao.uni");
  5. intent1.putExtra("param3", true);
  6. sendBroadcast(intent1);

📢 注意!!!Android 接收不到静态广播(自定义广播)

解决方法:intent1.setPackage(“目标包名”); 从Android 8.0(API级别26)开始,该系统对声明清单的接收者施加了额外的限制。 如果你的应用目标是Android 8.0或更高版本,你不能使用清单为大多数隐式广播(不是专门针对你的应用的广播)声明接收器。当用户正在使用你的应用程序时,你仍然可以使用上下文注册的接收器。

大致意思是官方在这方面做了限制,定义的静态广播必须指定范围(应用),类似于生活中广播要在指定频道才能收到。 既然如此,其实指定要广播的应用包名就行了,这样更加灵活和方便 ———————————————— 版权声明:本文为CSDN博主「飞鸭传书」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/jppipai/article/details/123270606

参考