安卓9.0系统修改定制化____修改rom 实现给指定app应用root权限

             在一些定制化项目中。有些客户需要给指定的app以root权限运行。那么如何修改系统固件。实现开机自动给指定的应用root权限需要我们了解基本的面具root权限常识以及修改的操作步骤。其他安卓版本可以参阅修改步骤。原理都是相同的。

通过博文了解💝💝💝

1💝💝💝-----修改rom 实现开机给指定应用root权限的全部步骤演示

2💝💝💝----修改此定制功能的SELinux 策略权限注意事项

3💝💝💝----修改rom  指定app授权root的必备条件

一。首先创建脚本目录

 在解包后的

import subprocess import re import time def run_adb_command(command): """执行ADB命令并返回输出""" try: result = subprocess.run( command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True ) if result.returncode != 0: return None return result.stdout.strip() except: return None def get_foreground_activity(): """获取前台应用Activity名称""" # 根据Android版本选择不同的命令(引用[2]) android_version = run_adb_command("adb shell getprop ro.build.version.release") if android_version and float(android_version[:3]) >= 9.0: command = "adb shell dumpsys activity activities | grep mResumedActivity" else: command = "adb shell dumpsys activity activities | grep mFocusedActivity" output = run_adb_command(command) if not output: return None # 解析Activity名称 match = re.search(r&#39;(\S+)/&#39;, output) return match.group(1) if match else None def get_app_fps(package_name): """获取应用帧率(FPS)""" # 方法1: 使用gfxinfo命令(需要开发者选项中开启GPU渲染分析) gfx_output = run_adb_command(f"adb shell dumpsys gfxinfo {package_name}") if gfx_output: # 解析帧时间数据 frame_times = re.findall(r&#39;(\d+\.\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)&#39;, gfx_output) if frame_times: # 计算最近帧的平均FPS total_frames = len(frame_times) total_time = sum(float(frame[2]) for frame in frame_times) / 1000 # 转换为秒 return round(total_frames / total_time, 1) if total_time > 0 else 0 # 方法2: 使用SurfaceFlinger(引用[1]) surface_output = run_adb_command(f"adb shell dumpsys SurfaceFlinger | grep {package_name}") if surface_output: # 解析帧率数据 match = re.search(r&#39;(\d+) fps&#39;, surface_output) if match: return int(match.group(1)) return None def get_surface_fps(): """直接获取SurfaceFlinger的全局帧率""" output = run_adb_command("adb shell dumpsys SurfaceFlinger --latency") if output: lines = output.split(&#39;\n&#39;) if len(lines) > 2: timestamps = [int(line.split()[0]) for line in lines[1:] if line.strip()] if len(timestamps) > 1: # 计算帧间隔时间 time_diff = (timestamps[-1] - timestamps[0]) / 1e9 # 纳秒转秒 return round((len(timestamps) - 1) / time_diff, 1) return None def get_refresh_rate(): """获取屏幕刷新率(Hz)""" # 方法1: 通过display服务 display_output = run_adb_command("adb shell dumpsys display | grep mRefreshRate") if display_output: match = re.search(r&#39;mRefreshRate=([\d.]+)&#39;, display_output) if match: return float(match.group(1)) # 方法2: 通过SurfaceFlinger surface_output = run_adb_command("adb shell dumpsys SurfaceFlinger | grep refresh-rate") if surface_output: match = re.search(r&#39;refresh-rate: ([\d.]+)&#39;, surface_output) if match: return float(match.group(1)) # 方法3: 通过wm命令 wm_output = run_adb_command("adb shell wm density") if wm_output: match = re.search(r&#39;Physical size: \d+x\d+ @ ([\d.]+)&#39;, wm_output) if match: return float(match.group(1)) return 60.0 # 默认值 def get_display_info(): """获取完整的显示信息""" fps = get_surface_fps() or get_app_fps(get_foreground_activity()) refresh_rate = get_refresh_rate() return { "app_fps": fps, "refresh_rate": refresh_rate, "vsync_missed": fps and refresh_rate and (fps < refresh_rate * 0.9) } if __name__ == "__main__": # 示例使用 package = "com.android.settings" # 替换为目标包名 print("正在获取性能数据...") # 获取前台应用 foreground_app = get_foreground_activity() print(f"前台应用: {foreground_app or &#39;未知&#39;}") # 获取帧率 app_fps = get_app_fps(package) surface_fps = get_surface_fps() print(f"应用帧率(FPS): {app_fps or &#39;N/A&#39;}") print(f"Surface帧率(FPS): {surface_fps or &#39;N/A&#39;}") # 获取刷新率 refresh_rate = get_refresh_rate() print(f"屏幕刷新率: {refresh_rate} Hz") # 综合信息 display_info = get_display_info() print(f"显示同步状态: {&#39;正常&#39; if not display_info[&#39;vsync_missed&#39;] else &#39;VSync丢失&#39;}") #!/usr/bin/env python3 # -*- coding: gbk -*- # 声明GBK编码 import subprocess import re import time import csv from datetime import datetime class DeviceMonitor: def __init__(self, interval=5, output_file="device_performance.csv"): self.interval = interval # 监控间隔(秒) self.output_file = output_file self.header_written = False def check_adb_connection(self): """验证ADB设备连接状态""" try: result = subprocess.run( "adb devices", shell=True, capture_output=True, text=True, encoding=&#39;gbk&#39; ) if "device" not in result.stdout: print("设备未连接或未授权") return False return True except Exception as e: print(f"ADB连接检查失败: {str(e)}") return False def get_foreground_package(self): """获取当前前台应用包名""" try: # 方法1: 使用dumpsys window命令 result = subprocess.run( "adb shell dumpsys window windows", shell=True, capture_output=True, text=True, encoding=&#39;gbk&#39;, errors=&#39;ignore&#39; ) output = result.stdout # 尝试多种匹配模式 patterns = [ r&#39;mCurrentFocus=Window\{.*?\s([a-zA-Z0-9._]+)/&#39;, r&#39;mFocusedApp=AppWindowToken\{.*?\s([a-zA-Z0-9._]+)/&#39;, r&#39;ResumedActivity: ActivityRecord\{.*?\s([a-zA-Z0-9._]+)/&#39; ] for pattern in patterns: match = re.search(pattern, output) if match: return match.group(1) # 方法2: 使用dumpsys activity命令(备用) result = subprocess.run( "adb shell dumpsys activity activities", shell=True, capture_output=True, text=True, encoding=&#39;gbk&#39;, errors=&#39;ignore&#39; ) match = re.search(r&#39;ResumedActivity: ActivityRecord\{.*?\s([a-zA-Z0-9._]+)/&#39;, result.stdout) return match.group(1) if match else "unknown" except Exception as e: print(f"获取前台应用失败: {str(e)}") return "unknown" def get_cpu_usage(self): """获取CPU使用率(%)""" try: # 获取总CPU时间和空闲时间 result = subprocess.run( "adb shell top -n 1 -d 0.5 | grep {match.group(1)}", shell=True, capture_output=True, text=True ) cpu_stats = result.stdout.split()[1:] total_time = sum(map(int, cpu_stats)) idle_time = int(cpu_stats[3]) # 计算使用率 usage = 100 * (1 - idle_time / total_time) return round(usage, 1) except: return 0.0 def get_memory_usage(self): """获取内存使用情况(MB)""" try: result = subprocess.run( "adb shell cat /proc/meminfo", shell=True, capture_output=True, text=True ) meminfo = result.stdout.splitlines() total = int(meminfo[0].split()[1]) // 1024 free = int(meminfo[1].split()[1]) // 1024 used = total - free return total, used, free except: return 0, 0, 0 def get_battery_info(self): """获取电池信息和温度(℃)""" try: result = subprocess.run( "adb shell dumpsys battery", shell=True, capture_output=True, text=True ) output = result.stdout level = re.search(r&#39;level: (\d+)&#39;, output) status = re.search(r&#39;status: (\d+)&#39;, output) temp = re.search(r&#39;temperature: (\d+)&#39;, output) return ( int(level.group(1)) if level else 0, int(status.group(1)) if status else 0, int(temp.group(1)) / 10.0 if temp else 0.0 # 转换为摄氏度 ) except: return 0, 0, 0.0 def get_cpu_temperature(self): """获取CPU温度(℃) - 需要root权限""" try: result = subprocess.run( "adb shell cat /sys/class/thermal/thermal_zone0/temp", shell=True, capture_output=True, text=True ) return float(result.stdout.strip()) / 1000.0 except: return 0.0 def monitor_performance(self): """主监控循环""" if not self.check_adb_connection(): print("设备连接失败,请检查ADB连接") return print(f"开始设备监控,间隔: {self.interval}秒") print("按Ctrl+C停止监控...") try: while True: timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") package = self.get_foreground_package() cpu_usage = self.get_cpu_usage() mem_total, mem_used, mem_free = self.get_memory_usage() batt_level, batt_status, batt_temp = self.get_battery_info() cpu_temp = self.get_cpu_temperature() # 输出到控制台 print(f"[{timestamp}] 应用: {package[:20]:<20} | " f"CPU: {cpu_usage}% | " f"内存: {mem_used}/{mem_total}MB | " f"电池: {batt_level}% | " f"温度: CPU:{cpu_temp:.1f}℃ 电池:{batt_temp:.1f}℃") # 保存到CSV文件 self.save_to_csv(timestamp, package, cpu_usage, mem_total, mem_used, mem_free, batt_level, batt_status, batt_temp, cpu_temp) time.sleep(self.interval) except KeyboardInterrupt: print("\n监控已停止") def save_to_csv(self, timestamp, package, cpu_usage, mem_total, mem_used, mem_free, batt_level, batt_status, batt_temp, cpu_temp): """保存数据到CSV文件""" data = [ timestamp, package, cpu_usage, mem_total, mem_used, mem_free, batt_level, batt_status, batt_temp, cpu_temp ] with open(self.output_file, &#39;a&#39;, newline=&#39;&#39;, encoding=&#39;utf-8&#39;) as f: writer = csv.writer(f) if not self.header_written: writer.writerow([ &#39;时间戳&#39;, &#39;前台应用&#39;, &#39;CPU使用率(%)&#39;, &#39;内存总量(MB)&#39;, &#39;已用内存(MB)&#39;, &#39;空闲内存(MB)&#39;, &#39;电池电量(%)&#39;, &#39;电池状态&#39;, &#39;电池温度(℃)&#39;, &#39;CPU温度(℃)&#39; ]) self.header_written = True writer.writerow(data) if __name__ == "__main__": # 创建监控实例并启动(每10秒监控一次) monitor = DeviceMonitor(interval=10) monitor.monitor_performance() 将此两个脚本结合起来
06-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安卓机器

如果感觉对你有点用处,请适打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值