android 中静默安装APK实现代码

    xiaoxiao2021-04-16  31

    /* ** ** Copyright 2007, The Android Open Source Project ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** **     http://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. */ package com.android.packageinstaller; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.RemoteException; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.lang.reflect.Method; import java.nio.charset.Charset; import android.content.pm.IPackageManager; import android.content.pm.IPackageInstallObserver2; import android.content.pm.VerificationParams; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; import android.os.IBinder; import android.util.Log; public class SilentInstallReceiver extends BroadcastReceiver { private final String TAG = "SilentInstallReceiver"; private String InstallAction = "mia_control_installApp"; Context mContext;     @Override     public void onReceive(Context context, Intent intent) {     mContext = context;     String action = intent.getAction();     String path = intent.getStringExtra("apkName");     Log.i(TAG,"action :"+action);     Log.i(TAG,"path :"+path);             if (InstallAction.equals(action)) {         install(path);         }     }          public void install(String path)   {      File fileName = new File(path);            try {        if(fileName.exists()){          Class<?> clazz = Class.forName("android.os.ServiceManager"); Method method = clazz.getMethod("getService", String.class);   IBinder iBinder = (IBinder) method.invoke(null, "package");    IPackageManager ipm = IPackageManager.Stub.asInterface(iBinder);   @SuppressWarnings("deprecation")        VerificationParams verificationParams = new VerificationParams(null, null, null, VerificationParams.NO_UID, null); ipm.installPackage(fileName.getPath(), new PackageInstallObserver(), 2, null, verificationParams, null); } } catch (Exception e) {        // TODO Auto-generated catch block        e.printStackTrace();   Log.i(TAG,"Exception :"+e); }          }      class PackageInstallObserver extends IPackageInstallObserver2.Stub {        @Override     public void onUserActionRequired(Intent intent) throws RemoteException {        // TODO Auto-generated method stub        }        @Override     public void onPackageInstalled(String basePackageName, int returnCode, String msg, Bundle extras) throws RemoteException {        Log.i(TAG,"onPackageInstalled() basePackageName :"+basePackageName); Log.i(TAG,"onPackageInstalled() returnCode :"+returnCode); Log.i(TAG,"onPackageInstalled() msg :"+msg); MysendBroadcast(basePackageName,returnCode); Log.e("keven","basePackageName =="+basePackageName+"returnCode =="+returnCode); if ("com.cnlaunch.x431.ProMiNi".equals(basePackageName) && returnCode == 1) { Log.e("keven","*********"); Intent mIntent = new Intent(); ComponentName cn = new ComponentName( "com.cnlaunch.x431.ProMiNi", "com.cnlaunch.x431.ProMiNi.WelcomeActivity"); mIntent.setComponent(cn); mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); mContext.startActivity(mIntent); } }   }

    以上是实现代码,通过发广播的形式将安装apk路径以广播的形式发送,这样就实现了静默安装

    转载请注明原文地址: https://ju.6miu.com/read-672633.html

    最新回复(0)