安卓系统签名apk静默升级方法

实现静默安装首先手机就有root权限或者是system应用(使用厂商提供的签名文件签名过)

下面是实现静默升级的步骤:

1、在AndroidManifest文件中添加权限

<uses-permission

android:name="android.permission.INSTALL_PACKAGES"

tools:ignore="ProtectedPermissions" />

并且在application标签里面加入属性:android:sharedUserId="android.uid.system"

2、在apk文件下载完成后,首先进行修改权限操作

chmod("777",filePath);

public static void chmod(String permission, String path) {

    String command = "chmod " + permission + " " + path;

    Runtime runtime = Runtime.getRuntime();

    try {

        runtime.exec(command);

    } catch (IOException e) {

        e.printStackTrace();

    }

}

3、调用静默安装函数

// 静默安装,1-安装成功,或没有升级文件,2-升级安装出现异常,-1-程序异常

public static int installBySlient(Context context, String filePath) {

    int result = 0;

    try {

        File file = new File(filePath);

        if (filePath == null || filePath.length() == 0

        || (file = new File(filePath)) == null

        || file.length() <= 0 || !file.exists() || !file.isFile()) {

        return 1;

        }

    String[] args = {"pm", "install", "-r", filePath};

    ProcessBuilder processBuilder = new ProcessBuilder(args);


    Process process = null;

    BufferedReader successResult = null;

    BufferedReader errorResult = null;

    StringBuilder successMsg = new StringBuilder();

    StringBuilder errorMsg = new StringBuilder();


    try {

       process = processBuilder.start();

        successResult = new BufferedReader(new InputStreamReader(

        process.getInputStream()));

        errorResult = new BufferedReader(new InputStreamReader(

        process.getErrorStream()));

        String s;


        while ((s = successResult.readLine()) != null) {

            successMsg.append(s);

        }


        while ((s = errorResult.readLine()) != null) {

            errorMsg.append(s);

        }

    } catch (IOException e) {

        e.printStackTrace();

        result = 2;

    } catch (Exception e) {

        e.printStackTrace();

        result = 2;

    } finally {

        try {

            if (successResult != null) {

                successResult.close();

            }

            if (errorResult != null) {

                errorResult.close();

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

        if (process != null) {

            process.destroy();

        }

    }


    if (successMsg.toString().contains("Success")

    || successMsg.toString().contains("success")) {

        result = 1;

    } else {

        result = 2;

    }


    } catch (Exception e) {

        result = -1;

    }

    return result;

}

到这里,静默安装就已经完成了,但是这样有点美中不足,apk升级完成后不会自动启动,我们可以监听系统发送的广播,使用静态注册广播的方式,实现apk自启动

1、在需要启动的Activity中添加静态广播接收类

public static class UpdateRestartReceiver extends BroadcastReceiver {

@Override

    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals("android.intent.action.PACKAGE_REPLACED")) {

            Toast.makeText(context, "已升级到新版本", Toast.LENGTH_SHORT).show();

            Intent intent2 = new Intent(context, LivePlayerActivity.class);

           intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            context.startActivity(intent2);

    }

}

2、接着,在AndroidManifest中注册广播

<receiver android:name=".iptv.LivePlayerActivity$UpdateRestartReceiver">

<intent-filter>

<action android:name="android.intent.action.PACKAGE_REPLACED" />

<data android:scheme="package" />

</intent-filter>

</receiver>

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Android 11 中,可以使用 PackageInstaller API 来实现应用程序的静默升级。以下是实现静默升级的步骤: 1. 获取应用程序的 APK 文件。 2. 创建 PackageInstaller.SessionParams 对象。 3. 调用 PackageInstaller.createSession() 方法创建一个会话。 4. 通过会话 ID 打开输出流,并将 APK 文件写入输出流中。 5. 启动会话,等待应用程序安装完成。 以下是一个简单的示例代码: ```java private void installPackageSilently(String apkPath) { // 获取应用程序的 APK 文件 File apkFile = new File(apkPath); // 创建 PackageInstaller.SessionParams 对象 PackageInstaller.SessionParams params = new PackageInstaller.SessionParams( PackageInstaller.SessionParams.MODE_FULL_INSTALL); // 调用 PackageInstaller.createSession() 方法创建一个会话 PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller(); int sessionId = packageInstaller.createSession(params); try { // 通过会话 ID 打开输出流,并将 APK 文件写入输出流中 PackageInstaller.Session session = packageInstaller.openSession(sessionId); OutputStream out = session.openWrite("app", 0, -1); FileInputStream in = new FileInputStream(apkFile); byte[] buffer = new byte[65536]; int c; while ((c = in.read(buffer)) != -1) { out.write(buffer, 0, c); } session.fsync(out); in.close(); out.close(); // 启动会话,等待应用程序安装完成 session.commit(createIntentSender(context, sessionId)); } catch (IOException e) { e.printStackTrace(); } } private IntentSender createIntentSender(Context context, int sessionId) { Intent intent = new Intent(context, getClass()); intent.putExtra(EXTRA_SESSION_ID, sessionId); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); return pendingIntent.getIntentSender(); } ``` 需要注意的是,静默升级需要在系统签名的应用程序中运行,并且需要 android.permission.INSTALL_PACKAGES 权限。另外,如果应用程序已经在运行,则静默升级可能会失败。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值