package com.android.systemui; import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import java.lang.Process; import android.os.SystemProperties; import android.util.Log; import android.content.ComponentName; import android.os.Environment; import android.os.storage.StorageManager; import java.util.List; import android.os.storage.StorageVolume; import java.io.File; import android.net.Uri; import java.io.DataOutputStream; import java.io.IOException; import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; public class BootonAppReceiver extends BroadcastReceiver { private static final String TAG = "kickpi"; public static void controlLED() { new Thread(() -> { Process process = null; DataOutputStream outputStream = null; try { // 获取 Root 权限 process = Runtime.getRuntime().exec("su"); outputStream = new DataOutputStream(process.getOutputStream()); String baseDir = "/sys/class/leds/work-led/"; // 设置触发模式为 timer outputStream.writeBytes("echo timer > " + baseDir + "trigger\n"); // 设置亮灯时间(单位:ms) outputStream.writeBytes("echo 2000 > " + baseDir + "delay_on\n"); // 设置灭灯时间(单位:ms) outputStream.writeBytes("echo 2000 > " + baseDir + "delay_off\n"); outputStream.flush(); // 等待命令执行完成 Thread.sleep(100); int exitCode = process.waitFor(); if (exitCode == 0) { Log.d(TAG, "LED control successful!"); } else { Log.e(TAG, "LED control failed with exit code: " + exitCode); } } catch (IOException | InterruptedException e) { Log.e(TAG, "Error in LED control: " + e.getMessage()); } finally { try { if (outputStream != null) outputStream.close(); if (process != null) process.destroy(); } catch (IOException e) { Log.e(TAG, "Error cleaning up: " + e.getMessage()); } } }).start(); } static void AgingTestbootApp(Context context) { try { Intent myintent = new Intent(Intent.ACTION_MAIN); String bootAppPack = "null"; String bootAppClass = "null"; bootAppPack = "com.oranth.factory"; bootAppClass = "com.oranth.factory.MainActivity"; Log.i(TAG,"will boot up APP - " + bootAppPack + "/" + bootAppClass); ComponentName cn = new ComponentName(bootAppPack, bootAppClass); myintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); myintent.setComponent(cn); context.startActivity(myintent); } catch (Exception e) { e.printStackTrace(); } } static void LauncherbootApp(Context context) { try { Intent myintent = new Intent(Intent.ACTION_MAIN); String bootAppPack = "null"; String bootAppClass = "null"; if ("1".equals(SystemProperties.get("persist.sys.bootonDeviceTest", "0"))) { bootAppPack = "com.DeviceTest"; bootAppClass = "com.DeviceTest.DeviceTest"; SystemProperties.set("persist.sys.bootonDeviceTest", "0"); } else { bootAppPack = SystemProperties.get("persist.sys.bootAppPack", "null"); bootAppClass = SystemProperties.get("persist.sys.bootAppClass", "null"); } Log.i("kickpi","will boot up APP - " + bootAppPack + "/" + bootAppClass); ComponentName cn = new ComponentName(bootAppPack, bootAppClass); myintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); myintent.setComponent(cn); context.startActivity(myintent); } catch (Exception e) { e.printStackTrace(); } } // 静态标志位,记录是否已找到文件 private static boolean kickPiFound = false; @Override public void onReceive(Context context, Intent intent) { if (kickPiFound) { return; } Log.i("kickpi","intent.getAction() = " + intent.getAction()); Log.i("kickpi","Intent.ACTION_MEDIA_MOUNTED = " + Intent.ACTION_MEDIA_MOUNTED); if (Intent.ACTION_MEDIA_MOUNTED.equals(intent.getAction())) { Uri uri = intent.getData(); String path = uri.getPath(); Log.d(TAG, "path: " + path); // 构造目标文件路径 File kickPiFile = new File(path, "kickpi"); // 检查文件是否存在 if (kickPiFile.exists()) { // 设置标志位,后续广播将被忽略 kickPiFound = true; AgingTestbootApp(context); controlLED(); } } Log.i("kickpi","intent.getAction() = " + intent.getAction()); if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { if (!kickPiFound) { Log.i("kickpi","LauncherbootApp"); LauncherbootApp(context);} } } }