最近在Android 4.0.4系統下實現apk的靜默安裝和啟動的功能,這裡和大家分享一下,希望能有所幫助。
源碼如下:
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
public class InstallApkUtils {
public static void installAndStartApk(final Context context, final String apkPath) {
if ((apkPath==null) || (context==null)) {
return;
}
File file = new File(apkPath);
if (file.exists() == false) {
return;
}
new Thread() {
public void run() {
String packageName = getUninstallApkPackageName(context, apkPath);
if (silentInstall(apkPath)) {
List<ResolveInfo> matches = findActivitiesForPackage(context, packageName);
if ((matches!=null) && (matches.size()>0)) {
ResolveInfo resolveInfo = matches.get(0);
ActivityInfo activityInfo = resolveInfo.activityInfo;
startApk(activityInfo.packageName, activityInfo.name);
}
}
};
}.start();
}
public static String getUninstallApkPackageName(Context context, String apkPath) {
String packageName = null;
if (apkPath == null) {
return packageName;
}
PackageManager pm = context.getPackageManager();
PackageInfo info = pm.getPackageArchiveInfo(apkPath,
PackageManager.GET_ACTIVITIES);
if (info == null) {
return packageName;
}
packageName = info.packageName;
return packageName;
}
public static List<ResolveInfo> findActivitiesForPackage(Context context, String packageName) {
final PackageManager pm = context.getPackageManager();
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mainIntent.setPackage(packageName);
final List<ResolveInfo> apps = pm.queryIntentActivities(mainIntent, 0);
return apps != null ? apps : new ArrayList<ResolveInfo>();
}
public static boolean silentInstall(String apkPath) {
String cmd1 = "chmod 777 " + apkPath + " \n";
String cmd2 = "LD_LIBRARY_PATH=/vendor/lib:/system/lib pm install -r " + apkPath + " \n";
return execWithSID(cmd1, cmd2);
}
private static boolean execWithSID(String... args) {
boolean isSuccess = false;
Process process = null;
OutputStream out = null;
try {
process = Runtime.getRuntime().exec("su");
out = process.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(out);
for (String tmp : args) {
dataOutputStream.writeBytes(tmp);
}
dataOutputStream.flush(); // 提交命令
dataOutputStream.close(); // 關閉流操作
out.close();
isSuccess = waitForProcess(process);
} catch (IOException e) {
e.printStackTrace();
}
return isSuccess;
}
public static boolean startApk(String packageName, String activityName) {
boolean isSuccess = false;
String cmd = "am start -n " + packageName + "/" + activityName + " \n";
try {
Process process = Runtime.getRuntime().exec(cmd);
isSuccess = waitForProcess(process);
} catch (IOException e) {
NLog.i(TAG, e.getMessage());
e.printStackTrace();
}
return isSuccess;
}
private static boolean waitForProcess(Process p) {
boolean isSuccess = false;
int returnCode;
try {
returnCode = p.waitFor();
switch (returnCode) {
case 0:
isSuccess = true;
break;
case 1:
break;
default:
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return isSuccess;
}
}
如果要使用,還需以下步驟:
1、在AndroidManifest.xml文件裡添加如下權限:
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
2、進行系統簽名。命令如下:
java -jar signapk.jar platform.x509.pem platform.pk8 XXX.apk Signed_XXX.apk
好了,現在大功告成!!!
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11