美文网首页
android系统签名后静默安装和安装后自动重启

android系统签名后静默安装和安装后自动重启

作者: hl330 | 来源:发表于2019-04-15 14:56 被阅读0次

原生安卓6.0 ,有系统签名.下载好apk拿到path。

//系统签名下静默安装
    public static boolean systemInstall(PackageManager packageManager, String apkPath) {
        Log.d(TAG, apkPath);

        Class<?> pm = packageManager.getClass();
        try {
            if (Build.VERSION.SDK_INT >= 21) {
                Class<?> Install= Class.forName("android.app.PackageInstallObserver");
                Constructor<?> constructor = Install.getDeclaredConstructor();
                constructor.setAccessible(true);
                Object installObserver = constructor.newInstance();
                Method method = pm.getDeclaredMethod("installPackage", Uri.class, Install, int.class, String.class);
                method.setAccessible(true);
                method.invoke(packageManager, Uri.fromFile(new File(apkPath)), installObserver, 2, null);
            } else {
                Method method = pm.getDeclaredMethod("installPackage", Uri.class, Class.forName("android.content.pm.IPackageInstallObserver"), int.class, String.class);
                method.setAccessible(true);
                method.invoke(packageManager, Uri.fromFile(new File(apkPath)), null, 2, null);
            }
            return true;
        } catch (Exception e) {
          Log.e("error", e.getLocalizedMessage());
        }
        return false;
    }

接收安装应用的广播

public class InstallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // 接收安装广播
   Intent intent2 = new Intent();
        ComponentName componentName = new ComponentName(
                "***",  //被执行启动操作app的包名
                "com.**.**.MainActivity");   //MainActivity路径
        intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//
        intent2.setComponent(componentName);
        context.startActivity(intent2);
    }
}

AndroidManifest.xml 加上 权限和Receiver,
manifest 标签加android:sharedUserId="android.uid.system"

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:sharedUserId="android.uid.system"
    package="*.*.*">
   <uses-permission android:name="android.permission.DELETE_PACKAGES" />
    <uses-permission android:name="android.permission.INSTALL_PACKAGES" />

 <application
 ...
 >

            <receiver   
            android:name=".*.*.InstallReceiver"
            android:enabled="true"
            android:exported="true"
            android:priority="1000">
            <intent-filter>
                <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
            </intent-filter>
        </receiver>
< /application>
</manifest>

当更新完app后,会收到广播。重新拉起App

相关文章

网友评论

      本文标题:android系统签名后静默安装和安装后自动重启

      本文链接:https://www.haomeiwen.com/subject/ouuuwqtx.html