Android9.0靜默安裝(需要系統簽名)
package xxxx.xxx.xxx.xxx; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageInstaller;import android.content.pm.PackageInstaller.Session; import android.content.pm.PackageInstaller.SessionParams; import android.util.Log; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @TargetApi(27) public class NewPackageInstaller implements NewPackageManager.IPackageInstall { private Context mContext; private String mAction; public NewPackageInstaller(Context context) { this(context, "com.snack.content.INSTALL"); } public NewPackageInstaller(Context context, String action) {this.mContext = context; this.mAction = action; } private int createSession(PackageInstaller packageInstaller, SessionParams sessionParams) { int sessionId = -1; try { sessionId = packageInstaller.createSession(sessionParams); } catch (Exception var5) { var5.printStackTrace(); } return sessionId; } private boolean copyInstallFile(PackageInstaller packageInstaller, int sessionId, String appPath) { InputStream inputStream = null; OutputStream outputStream = null; Session session = null; boolean isSuccess = false; File appFile = new File(appPath); try { inputStream = new FileInputStream(appFile); session = packageInstaller.openSession(sessionId); outputStream = session.openWrite("my_app_session", 0L, appFile.length()); int total = 0; int len; for (byte[] buffer = new byte[4096]; (len = inputStream.read(buffer)) != -1; total += len) { outputStream.write(buffer, 0, len); } session.fsync(outputStream); boolean var12 = true; return var12; } catch (IOException var16) { var16.printStackTrace(); } finally { this.close(outputStream); this.close(inputStream); this.close(session); } return false; } @SuppressLint("WrongConstant") private boolean installPackage(PackageInstaller packageInstaller, int sessionId, NewPackageManager.IInstallObserver observer) { PendingIntent pendingIntent = null; Session session = null; try { session = packageInstaller.openSession(sessionId); IntentFilter ifliter = new IntentFilter(); ifliter.addAction(this.mAction); NewPackageInstaller.InstallReceiver installReceiver = new NewPackageInstaller.InstallReceiver(observer); this.mContext.registerReceiver(installReceiver, ifliter); Intent intent = new Intent(this.mAction); pendingIntent = PendingIntent.getBroadcast(this.mContext, sessionId, intent, 134217728); session.commit(pendingIntent.getIntentSender()); boolean var9 = true; return var9; } catch (IOException var13) { var13.printStackTrace(); } finally { this.close(session); } return false; } private void close(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (IOException var3) { var3.printStackTrace(); } } } public boolean installPackage(String appPath, NewPackageManager.IInstallObserver observer) { File appFile = new File(appPath); if (appFile.exists() && appFile.isFile() && appFile.canRead()) { PackageInstaller packageInstaller = this.mContext.getPackageManager().getPackageInstaller(); SessionParams sessionParams = new SessionParams(1); sessionParams.setSize(appFile.length()); int sessionId = this.createSession(packageInstaller, sessionParams); if (sessionId != -1) { boolean isCopySuccess = this.copyInstallFile(packageInstaller, sessionId, appPath); if (isCopySuccess) { return this.installPackage(packageInstaller, sessionId, observer); } } return false; } else { return false; } } class InstallReceiver extends BroadcastReceiver { private NewPackageManager.IInstallObserver mInstallObserver; public InstallReceiver(NewPackageManager.IInstallObserver observer) { this.mInstallObserver = observer; } public void onReceive(Context context, Intent intent) { Log.d("install", "receiver :" + intent.getAction() + ",intent.toString:" + intent.toString()); String pkgName = intent.getStringExtra("android.content.pm.extra.PACKAGE_NAME"); String result = intent.getStringExtra("android.content.pm.extra.STATUS_MESSAGE"); int resultCode = intent.getIntExtra("android.content.pm.extra.LEGACY_STATUS", 0); if (this.mInstallObserver != null) { this.mInstallObserver.onPackageInstalled(pkgName, resultCode, result); } context.unregisterReceiver(this); } } }
package xxx.xxx.xxx; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.util.Log; import java.util.Iterator; import java.util.List; public class NewPackageManager { private Context mContext; private PackageManager mPackageManager; private NewPackageManager.IAppObserver mDefaultAppObserver; private NewPackageManager.IPackageInstall mPackageInstaller; public NewPackageManager(Context context) { this.mContext = context.getApplicationContext(); this.mPackageManager = context.getPackageManager(); this.mDefaultAppObserver = new NewPackageManager.DefaultAppObserver(); if (android.os.Build.VERSION.SDK_INT > 27) { this.mPackageInstaller = new NewPackageInstaller(this.mContext); } } public boolean installPackage(String appFilePath, NewPackageManager.IInstallObserver installObserver) { return this.mPackageInstaller.installPackage(appFilePath, installObserver); } public static boolean isAvailable(PackageManager packageManager, String packageName) { List<PackageInfo> infos = packageManager.getInstalledPackages(0); Iterator infoIterator = infos.iterator(); PackageInfo info; do { if (!infoIterator.hasNext()) { return false; } info = (PackageInfo) infoIterator.next(); } while (!info.packageName.equals(packageName)); return true; } public interface IPackageInstall { boolean installPackage(String var1, NewPackageManager.IInstallObserver var2); } public interface IDeleteObserver { void onPackageDeleted(String var1, int var2, String var3); } public interface IInstallObserver { void onPackageInstalled(String var1, int var2, String var3); } public interface IAppObserver extends NewPackageManager.IInstallObserver, NewPackageManager.IDeleteObserver { } public static class DefaultAppObserver implements NewPackageManager.IAppObserver { public static final String INSTALL_TAG = "install"; public static final String DELETE_TAG = "delete"; public DefaultAppObserver() { } public void onPackageInstalled(String packageName, int returnCode, String msg) { if (returnCode == 1) { Log.d("install", "App install success: " + packageName); } else { Log.d("install", "Apk install fail! package:" + packageName + ". Error code:" + returnCode + "," + msg); } } public void onPackageDeleted(String packageName, int returnCode, String msg) { if (returnCode == 1) { Log.d("delete", "App delete success:" + packageName); } else { Log.d("install", "Apk delete fail! package:" + packageName + ". Error code:" + returnCode + "," + msg); } } } }
/** * Android9.0靜默安裝 */ /** * install app * the permission {#android.permission.INSTALL_PACKAGES} is only granted to * system apps * <p> * add #android:sharedUserId="android.uid.system"# to manifest * * @param appFilePath */ public static boolean installPackage(Context context, String appFilePath) { NewPackageManager mPackageManager = new NewPackageManager(context); /** *@param appPath 需要安裝的APP的絕對路徑 */ boolean ret = mPackageManager.installPackage(appFilePath, new NewPackageManager.IInstallObserver() { /** *@param packageName 安裝APP的包名 *@param returnCode 0為安裝成功,其餘值為安裝失敗 *@param errorMsg 失敗的錯誤資訊 */ @Override public void onPackageInstalled(String packageName, int returnCode, String errorMsg) { Log.i("install", "packageName:" + packageName + ",returnCode:" + returnCode + ",errorMsg:" + errorMsg); } }); LogUtil.i("install", "installPackage ret = 0為安裝成功 ret " + ret); return ret; }
package xxxx.xxx.xxx.xxx;
import android.annotation.SuppressLint;import android.annotation.TargetApi;import android.app.PendingIntent;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.content.pm.PackageInstaller;import android.content.pm.PackageInstaller.Session;import android.content.pm.PackageInstaller.SessionParams;import android.util.Log;
import java.io.Closeable;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;
@TargetApi(27)public class NewPackageInstaller implements NewPackageManager.IPackageInstall { private Context mContext; private String mAction;
public NewPackageInstaller(Context context) { this(context, "com.snack.content.INSTALL"); }
public NewPackageInstaller(Context context, String action) { this.mContext = context; this.mAction = action; }
private int createSession(PackageInstaller packageInstaller, SessionParams sessionParams) { int sessionId = -1;
try { sessionId = packageInstaller.createSession(sessionParams); } catch (Exception var5) { var5.printStackTrace(); }
return sessionId; }
private boolean copyInstallFile(PackageInstaller packageInstaller, int sessionId, String appPath) { InputStream inputStream = null; OutputStream outputStream = null; Session session = null; boolean isSuccess = false; File appFile = new File(appPath);
try { inputStream = new FileInputStream(appFile); session = packageInstaller.openSession(sessionId); outputStream = session.openWrite("my_app_session", 0L, appFile.length()); int total = 0;
int len; for (byte[] buffer = new byte[4096]; (len = inputStream.read(buffer)) != -1; total += len) { outputStream.write(buffer, 0, len); }
session.fsync(outputStream); boolean var12 = true; return var12; } catch (IOException var16) { var16.printStackTrace(); } finally { this.close(outputStream); this.close(inputStream); this.close(session); }
return false; }
@SuppressLint("WrongConstant") private boolean installPackage(PackageInstaller packageInstaller, int sessionId, NewPackageManager.IInstallObserver observer) { PendingIntent pendingIntent = null; Session session = null;
try { session = packageInstaller.openSession(sessionId); IntentFilter ifliter = new IntentFilter(); ifliter.addAction(this.mAction); NewPackageInstaller.InstallReceiver installReceiver = new NewPackageInstaller.InstallReceiver(observer); this.mContext.registerReceiver(installReceiver, ifliter); Intent intent = new Intent(this.mAction); pendingIntent = PendingIntent.getBroadcast(this.mContext, sessionId, intent, 134217728); session.commit(pendingIntent.getIntentSender()); boolean var9 = true; return var9; } catch (IOException var13) { var13.printStackTrace(); } finally { this.close(session); }
return false; }
private void close(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (IOException var3) { var3.printStackTrace(); } }
}
public boolean installPackage(String appPath, NewPackageManager.IInstallObserver observer) { File appFile = new File(appPath); if (appFile.exists() && appFile.isFile() && appFile.canRead()) { PackageInstaller packageInstaller = this.mContext.getPackageManager().getPackageInstaller(); SessionParams sessionParams = new SessionParams(1); sessionParams.setSize(appFile.length()); int sessionId = this.createSession(packageInstaller, sessionParams); if (sessionId != -1) { boolean isCopySuccess = this.copyInstallFile(packageInstaller, sessionId, appPath); if (isCopySuccess) { return this.installPackage(packageInstaller, sessionId, observer); } }
return false; } else { return false; } }
class InstallReceiver extends BroadcastReceiver { private NewPackageManager.IInstallObserver mInstallObserver;
public InstallReceiver(NewPackageManager.IInstallObserver observer) { this.mInstallObserver = observer; }
public void onReceive(Context context, Intent intent) { Log.d("install", "receiver :" + intent.getAction() + ",intent.toString:" + intent.toString()); String pkgName = intent.getStringExtra("android.content.pm.extra.PACKAGE_NAME"); String result = intent.getStringExtra("android.content.pm.extra.STATUS_MESSAGE"); int resultCode = intent.getIntExtra("android.content.pm.extra.LEGACY_STATUS", 0); if (this.mInstallObserver != null) { this.mInstallObserver.onPackageInstalled(pkgName, resultCode, result); }
context.unregisterReceiver(this); } }}