当前位置: 首页 > article >正文

android 10 后台启动activity

摘要:Android 10(API 级别 29)及更高版本会限制应用何时可以启动 activity 背景。这些限制有助于最大限度地减少对用户的干扰, 让用户能够更好地控制其屏幕上显示的内容。本文以此为出发点,基于展锐平台对系统代码进行改造,通过白名单的方式允许部分应用可以从后台启动activity。
Google官方:
针对从后台启动 activity 的限制

1.添加白名单

Index: build/make/target/product/base_system.mk
===================================================================
--- build/make/target/product/base_system.mk	(版本 1957)
+++ build/make/target/product/base_system.mk	(版本 1959)
@@ -269,6 +269,9 @@
     wifi-service \
     wm \
 
+PRODUCT_PACKAGES += background-startactivity-package-whitelist.txt
+
 # VINTF data for system image
 PRODUCT_PACKAGES += \
     system_manifest.xml \

Index: frameworks/base/data/etc/background-startactivity-package-whitelist.txt
===================================================================
--- frameworks/base/data/etc/background-startactivity-package-whitelist.txt	(不存在的)
+++ frameworks/base/data/etc/background-startactivity-package-whitelist.txt	(版本 1959)
@@ -0,0 +1 @@
+com.android.permissioncontroller
Index: frameworks/base/data/etc/Android.bp
===================================================================
--- frameworks/base/data/etc/Android.bp	(版本 1957)
+++ frameworks/base/data/etc/Android.bp	(版本 1959)
@@ -133,3 +133,9 @@
     sub_dir: "permissions",
     src: "com.android.timezone.updater.xml",
 }
+
+prebuilt_etc {
+    name: "background-startactivity-package-whitelist.txt",
+    sub_dir: "permissions",
+    src: "background-startactivity-package-whitelist.txt",
+}

2. 实现方案

Index: frameworks/base/services/core/java/com/android/server/wm/ActivityStarter.java
===================================================================
--- frameworks/base/services/core/java/com/android/server/wm/ActivityStarter.java	(版本 1957)
+++ frameworks/base/services/core/java/com/android/server/wm/ActivityStarter.java	(版本 1959)
@@ -1064,6 +1064,14 @@
                     + " allowed because SYSTEM_ALERT_WINDOW permission is granted.");
             return false;
         }
+
+        // don't abort if the callingPackage on the whitelist
+        if (ActivityBackgroundStartCheckUtil.isWhitelistApp(callingPackage, intent)) {
+            Slog.w(TAG, "Background activity start for " + callingPackage
+                    + " allowed because it is on the whitelist.");
+            return false;
+        }
+
         // anything that has fallen through would currently be aborted
         Slog.w(TAG, "Background activity start [callingPackage: " + callingPackage
                 + "; callingUid: " + callingUid

Index: frameworks/base/services/core/java/com/android/server/wm/ActivityBackgroundStartCheckUtil.java
===================================================================
--- frameworks/base/services/core/java/com/android/server/wm/ActivityBackgroundStartCheckUtil.java	(不存在的)
+++ frameworks/base/services/core/java/com/android/server/wm/ActivityBackgroundStartCheckUtil.java	(版本 1959)
@@ -0,0 +1,100 @@
+package com.android.server.wm;
+
+
+import android.content.Context;
+import android.content.Intent;
+import android.os.Environment;
+import android.os.Process;
+import android.text.TextUtils;
+import android.util.Log;
+
+import com.android.internal.util.ArrayUtils;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.HashSet;
+
+public class ActivityBackgroundStartCheckUtil{
+
+    private static String TAG = "ActivityBackgroundStartCheckUtil";
+
+    private static final File GRANT_SYS_APP_LIST_SYSTEM = Environment
+            .buildPath(Environment.getRootDirectory(), "etc", "permissions",
+                    "background-startactivity-package-whitelist.txt");
+
+    private static HashSet<String> mWhitelistAppSet = new HashSet<String>();
+
+    private static String Key[] = {"android", "call"};
+
+    public static boolean isWhitelistApp(String callingPackage, Intent intent){
+        sGetGrantSystemAppFromFile(mWhitelistAppSet, GRANT_SYS_APP_LIST_SYSTEM);
+        Log.d(TAG, "isWhitelistApp callingPackage="+callingPackage);
+        try {
+            String packageName = intent.getComponent().getPackageName();
+            String className = intent.getComponent().getClassName();
+            if (mWhitelistAppSet.contains(callingPackage) ||
+                mWhitelistAppSet.contains(packageName)) {
+                return true;     
+            }
+            for (String key : Key) {
+                if (className.contains(key)) {
+                    return true; 
+                }
+            }
+        } catch (Exception e) {
+            //e.printStackTrace();
+            Log.d(TAG, e.getMessage());
+        }
+        return false;
+    }
+
+    
+    /**
+     * Get removable system app list from config file
+     *
+     * @param resultSet
+     *            Returned result list
+     * @param file
+     *            The config file
+     */
+    private static void sGetGrantSystemAppFromFile(
+            HashSet<String> resultSet, File file) {
+        resultSet.clear();
+        FileReader fr = null;
+        BufferedReader br = null;
+        try {
+            if (file.exists()) {
+                fr = new FileReader(file);
+            } else {
+                Log.d(TAG, "file in " + file + " does not exist!");
+                return;
+            }
+            br = new BufferedReader(fr);
+            String line;
+            while ((line = br.readLine()) != null) {
+                line = line.trim();
+                if (!TextUtils.isEmpty(line)) {
+                    Log.d(TAG, "read line " + line);
+                    resultSet.add(line);
+                }
+            }
+            Log.e(TAG,"GRANT_SYS_APP_LIST_SYSTEM size="+resultSet.size());
+        } catch (Exception io) {
+            Log.d(TAG, io.getMessage());
+        } finally {
+            try {
+                if (br != null) {
+                    br.close();
+                }
+                if (fr != null) {
+                    fr.close();
+                }
+            } catch (IOException io) {
+                Log.d(TAG, io.getMessage());
+            }
+        }
+    }
+}
+


http://www.kler.cn/a/372648.html

相关文章:

  • Leetcode224 -- 基本计算器及其拓展
  • 电子电气架构 --- 车载芯片现状
  • [0152].第3节:IDEA中工程与模块
  • 推荐系统中的AB测试
  • Docker-微服务项目部署
  • 一款专业获取 iOS 设备的 UDID 工具|一键获取iPhone iPad设备的 UDID
  • Unity Editor 快速移动资源
  • VB中如何创建和使用自定义控件
  • 【动手学电机驱动】 STM32-FOC(1)IHM03 电机控制套件的介绍
  • 微服务技术,SpringCloudAlibaba,Redis,RocketMQ,Docker,分库分表
  • 基于SSM+VUE园艺生活植物网站JAVA|VUE|Springboot计算机毕业设计源代码+数据库+LW文档+开题报告+答辩稿+部署教+代码讲解
  • 一七一、React性能优化方式
  • 计算机网络(Ⅴ)网络核心
  • 【ChatGPT】让ChatGPT帮助进行头脑风暴与创意生成
  • 指针进阶(四)(C 语言)
  • Java项目实战II基于Spring Boot的火锅店管理系统设计与实现(开发文档+数据库+源码)
  • gpio子系统-通过io来控制gpio
  • 详解:单例模式中的饿汉式和懒汉式
  • lego-loam mapOptmization 源码注释(一)
  • Lua语法基础全面剖析(中篇)
  • uni-app应用级生命周期和页面级生命周期
  • huggingface之tokenization基础结构Trie-代码解读
  • 【缓存与加速技术实践】Redis 主从复制
  • 银河麒麟v10安装Anaconda(python大蟒蛇)+pycharm安装
  • AJAX和JSON
  • K8S 容器可视化管理工具-kuboard 监控管理工具搭建