Android物联网高级指令汇总(Root,系统签名)

静默授权USB设备

依赖:Rxjava,系统签名

@SuppressLint("PrivateApi")
    public static boolean grantAutomaticPermission(Context context,UsbDevice usbDevice) {
        try {
            PackageManager pkgManager = context.getPackageManager();
            ApplicationInfo appInfo = pkgManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);

            Class<?> serviceManagerClass = Class.forName("android.os.ServiceManager");
            Method getServiceMethod = serviceManagerClass.getDeclaredMethod("getService", String.class);
            getServiceMethod.setAccessible(true);
            android.os.IBinder binder = (android.os.IBinder) getServiceMethod.invoke(null, Context.USB_SERVICE);

            Class<?> iUsbManagerClass = Class.forName("android.hardware.usb.IUsbManager");
            Class<?> stubClass = Class.forName("android.hardware.usb.IUsbManager$Stub");
            Method asInterfaceMethod = stubClass.getDeclaredMethod("asInterface", android.os.IBinder.class);
            asInterfaceMethod.setAccessible(true);
            Object iUsbManager = asInterfaceMethod.invoke(null, binder);


//            System.out.println("UID : " + appInfo.uid + " " + appInfo.processName + " " + appInfo.permission);
            final Method grantDevicePermissionMethod = iUsbManagerClass.getDeclaredMethod("grantDevicePermission", UsbDevice.class, int.class);
            grantDevicePermissionMethod.setAccessible(true);
            grantDevicePermissionMethod.invoke(iUsbManager, usbDevice, appInfo.uid);

            //Logger.i("Method OK : %s  %s%n", binder, iUsbManager);
            return true;
        } catch (Exception e) {
            Logger.e("Error trying to assing automatic usb permission :%s",e);
            return false;
        }
    }

##查询以太网状态
依赖:ROOT权限

cat /sys/class/net/eth0/carrier

正常0是拔出,1是插入

App操作

更新并启动APP
强行停止APP
清除数据并启动APP
重新启动APP

public class SuAppUtils {
    private final static String ENTRANCE_ACTIVITY = SplashActivity.class.getName();

    public static void stop(String packageName) {
        runCmd("am force-stop %s", packageName);
    }

    public static boolean upgrade(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return false;
        }

        String packageName = getPackageName();
        runCmd("chmod 777 %s", path);
        return runCmd("pm install -r %s && am start -S %s/%s", path, packageName, ENTRANCE_ACTIVITY);

    }

    private static String getPackageName() {
        return App.getAppContext().getPackageName();
    }

    private static boolean runCmd(String format, Object... args) {
        String cmd = String.format(format, args);
        CommandResult result = Shell.SU.run(cmd);
        if (result.exitCode != 0) {
            Logger.w(JsonUtils.toPretty(result.stderr));
            return false;
        }
        return true;
    }

    public static void reset() {
        String packageName = getPackageName();
        runCmd("pm clear %s && am start -S %s/%s", packageName, packageName, ENTRANCE_ACTIVITY);
    }

    public static void restart() {
        String packageName = getPackageName();
        runCmd("am start -S %s/%s", packageName, ENTRANCE_ACTIVITY);
    }


}

设置默认桌面

依赖:需要系统签名


public class LauncherUtils {
    public static void setDefaultLauncherApp(Context context, ComponentName componentName) {
        PackageManager pm = context.getPackageManager();
        Method[] methods = PackageManager.class.getDeclaredMethods();
        Method replacePreferredActivity = null;
        Method getHomeActivities = null;
        for (Method method : methods) {
            if (method.getName().equals("replacePreferredActivity")) {
                replacePreferredActivity = method;
            }
            if (method.getName().equals("getHomeActivities")) {
                getHomeActivities = method;
            }
        }
        replacePreferredActivity.setAccessible(true);
        getHomeActivities.setAccessible(true);

        IntentFilter homeFilter = new IntentFilter(Intent.ACTION_MAIN);
        homeFilter.addCategory(Intent.CATEGORY_HOME);
        homeFilter.addCategory(Intent.CATEGORY_DEFAULT);
        final List<ComponentName> allComponents = new ArrayList<>();
        final List<ResolveInfo> homeActivities = new ArrayList<>();
        try {
            getHomeActivities.invoke(pm, homeActivities);
            for (ResolveInfo info : homeActivities) {
                final ActivityInfo appInfo = info.activityInfo;
                ComponentName activityName = new ComponentName(appInfo.packageName, appInfo.name);
                allComponents.add(activityName);
            }
            replacePreferredActivity.invoke(pm, homeFilter, IntentFilter.MATCH_CATEGORY_EMPTY, allComponents.toArray(new ComponentName[0]), componentName);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static ComponentName listHomeActivities(Context context, List<ResolveInfo> outs){
        PackageManager pm = context.getPackageManager();
        Object cn = null;
        try {
            Class<?> packageManager = Class.forName("android.content.pm.PackageManager");
            Method getHomeActivities = packageManager.getMethod("getHomeActivities",List.class);
            getHomeActivities.setAccessible(true);
            cn = getHomeActivities.invoke(pm,outs);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return (ComponentName) cn;
    }


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值