在android中静默安装应用

首先,说明,为了安全是无法在所有的android 系统中进行静默安装的,我下面所讲的静默安装的前提是必须是系统的签名才行,好了,不多了,直接上代码。

第一步,添加一个安装回调接口

public interface OnInstalledPackaged {
	
	public void packageInstalled(String packageName, int returnCode);

}

第二步,定义一个系统不可见的接口IPackageInstallObserver,所以包名是不能变的

package android.content.pm;

public interface IPackageInstallObserver extends android.os.IInterface {
	
	public abstract static class Stub extends android.os.Binder implements android.content.pm.IPackageInstallObserver {
		public Stub() {
			throw new RuntimeException("Stub!");
		}

		public static android.content.pm.IPackageInstallObserver asInterface(android.os.IBinder obj) {
			throw new RuntimeException("Stub!");
		}

		public android.os.IBinder asBinder() {
			throw new RuntimeException("Stub!");
		}

		public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags)
				throws android.os.RemoteException {
			throw new RuntimeException("Stub!");
		}
	}

	public abstract void packageInstalled(java.lang.String packageName, int returnCode)
			throws android.os.RemoteException;
}
第三步,实现ApplicationManager功能

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.content.Context;
import android.content.pm.IPackageInstallObserver;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.RemoteException;

public class ApplicationManager {
	public final int INSTALL_REPLACE_EXISTING = 2;
    public static final int INSTALL_SUCCEEDED = 1;

	private PackageInstallObserver observer;
	private PackageManager pm;
	private Method method;
	
	private OnInstalledPackaged onInstalledPackaged;
	
    class PackageInstallObserver extends IPackageInstallObserver.Stub {

		public void packageInstalled(String packageName, int returnCode) throws RemoteException {
			if (onInstalledPackaged != null) {
				onInstalledPackaged.packageInstalled(packageName, returnCode);
			}
		}
	}
	
	public ApplicationManager(Context context) throws SecurityException, NoSuchMethodException {
		
        observer = new PackageInstallObserver();
        pm = context.getPackageManager();
        
        Class<?>[] types = new Class[] {Uri.class, IPackageInstallObserver.class, int.class, String.class};
		method = pm.getClass().getMethod("installPackage", types);
	}
	
	public void setOnInstalledPackaged(OnInstalledPackaged onInstalledPackaged) {
		this.onInstalledPackaged = onInstalledPackaged;
	}

	public void installPackage(String apkFile) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		installPackage(new File(apkFile));
	}
	
	public void installPackage(File apkFile) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		if (!apkFile.exists()) throw new IllegalArgumentException();
		Uri packageURI = Uri.fromFile(apkFile);
		installPackage(packageURI);
	}
	
	public void installPackage(Uri apkFile) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		method.invoke(pm, new Object[] {apkFile, observer, INSTALL_REPLACE_EXISTING, null});
	}
	
}

最后一步,就是测试安装()

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class Test extends Activity {

	public static final String TAG = "Test";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		try {
			final ApplicationManager am = new ApplicationManager(this);
			am.setOnInstalledPackaged(new OnInstalledPackaged() {
				public void packageInstalled(String packageName, int returnCode) {
					if (returnCode == ApplicationManager.INSTALL_SUCCEEDED) {
						Log.d(TAG, "Install succeeded");
					} else {
						Log.d(TAG, "Install failed: " + returnCode);
					}
				}
			});

			Button btnInstall = (Button) findViewById(R.id.app_name);
			btnInstall.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					try {
						am.installPackage("filepath");
					} catch (Exception e) {
						Log.e(TAG, "", e);
					}
				}
			});
		} catch (Exception e) {
			Log.e(TAG, "", e);
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值