当前位置: 代码迷 >> Android >> android 装配删除软件
  详细解决方案

android 装配删除软件

热度:15   发布时间:2016-05-01 10:21:35.0
android 安装删除软件

?1、 Android.mk文件

?

  1. LOCAL_PATH:=?$(call?my-dir)??
  2. include?$(CLEAR_VARS)??
  3. ??
  4. LOCAL_MODULE_TAGS?:=?optional??
  5. ??
  6. LOCAL_SRC_FILES?:=?$(call?all-subdir-java-files)??
  7. ??
  8. LOCAL_PACKAGE_NAME?:=?PackageInstaller??
  9. LOCAL_CERTIFICATE?:=?platform??
  10. ??
  11. include?$(BUILD_PACKAGE)??

2、PakcageInstaller.java文件(关键代码)

package cn.ceadic.apkmgr;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.util.Log;

import android.content.pm.IPackageInstallObserver;
import android.content.pm.IPackageDeleteObserver;
import android.os.FileUtils;


public class PackageInstaller {
?
?private File mTmpFile;
?private final String TMP_FILE_NAME = "tmpCopy.apk";

?private final static String TAG = "PackInstaller";
?private Context mContext;

?public PackageInstaller(Context context) {
??mContext = context;
?}

?
?public void install(String path,String packageName){
?? Intent intent = new Intent(Intent.ACTION_VIEW);
?? intent.setDataAndType(Uri.fromFile(new File(path)),
?? "application/vnd.android.package-archive");
?? mContext.startActivity(intent);
?}
?
?public void instatllBatch(String path, String packageName) {

??Log.i(TAG, "path=" + path);
??int installFlags = 0;
??PackageManager pm = mContext.getPackageManager();
??try {
???PackageInfo pi = pm.getPackageInfo(packageName,
?????PackageManager.GET_UNINSTALLED_PACKAGES);
???if (pi != null) {
????installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
???}
??} catch (NameNotFoundException e) {
??}
??if ((installFlags & PackageManager.INSTALL_REPLACE_EXISTING) != 0) {
???Log.w(TAG, "Replacing package:" + packageName);
??}

??// Create temp file before invoking install api
??mTmpFile = createTempPackageFile(path);
??if (mTmpFile == null) {
???// Message msg = mHandler.obtainMessage(INSTALL_COMPLETE);
???// msg.arg1 = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
???// mHandler.sendMessage(msg);
???return;
??}
??Uri mPackageURI = Uri.parse("file://" + mTmpFile.getPath());
??String installerPackageName = mContext.getIntent().getStringExtra(
????Intent.EXTRA_INSTALLER_PACKAGE_NAME);

??PackageInstallObserver observer = new PackageInstallObserver();
??pm.installPackage(mPackageURI, observer, installFlags,
????installerPackageName);
?}

?private File createTempPackageFile(String filePath) {
??File tmpPackageFile = mContext.getFileStreamPath(TMP_FILE_NAME);
??if (tmpPackageFile == null) {
???Log.w(TAG, "Failed to create temp file");
???return null;
??}
??if (tmpPackageFile.exists()) {
???tmpPackageFile.delete();
??}
??// Open file to make it world readable
??FileOutputStream fos;
??try {
???fos = openFileOutput(TMP_FILE_NAME, MODE_WORLD_READABLE);
??} catch (FileNotFoundException e1) {
???Log.e(TAG, "Error opening file " + TMP_FILE_NAME);
???return null;
??}
??try {
???fos.close();
??} catch (IOException e) {
???Log.e(TAG, "Error opening file " + TMP_FILE_NAME);
???return null;
??}

??File srcPackageFile = new File(filePath);
??if (!FileUtils.copyFile(srcPackageFile, tmpPackageFile)) {
???Log.w(TAG, "Failed to make copy of file: " + srcPackageFile);
???return null;
??}
??return tmpPackageFile;
?}

?private class PackageInstallObserver extends IPackageInstallObserver.Stub {
??public void packageInstalled(String packageName, int returnCode) {
???// Message msg = mHandler.obtainMessage(INSTALL_COMPLETE);
???// msg.arg1 = returnCode;
???// mHandler.sendMessage(msg);
???Log.i(TAG, "====INSTALL_COMPLETE");
??}
?}
?
?private class PackageDeleteObserver extends IPackageDeleteObserver.Stub {
??????? public void packageDeleted(boolean succeeded) {
//??????????? Message msg = mHandler.obtainMessage(UNINSTALL_COMPLETE);
//??????????? msg.arg1 = succeeded?SUCCEEDED:FAILED;
//??????????? mHandler.sendMessage(msg);
??????????? Log.i(TAG, "====UNINSTALL_COMPLETE");
??????? }
??? }
???
??? public void uninstall(String packageName){
??? ?Uri packageURI = Uri.parse("package:" + packageName);
??Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,
??packageURI);
??mContext.startActivity(uninstallIntent);
?}
?
?public void uninstallBatch(String packageName) {
???? PackageDeleteObserver observer = new PackageDeleteObserver();
??????? mContext.getPackageManager().deletePackage(packageName, observer, 0);
??
?}
}
3 添加权限.

  1. <uses-permission?android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>??
  2. ????<uses-permission?android:name="android.permission.INSTALL_PACKAGES"?/>??
  3. ????<uses-permission?android:name="android.permission.DELETE_PACKAGES"?/>??
  4. ????<uses-permission?android:name="android.permission.CLEAR_APP_CACHE"?/>??
  5. ????<uses-permission?android:name="android.permission.READ_PHONE_STATE"?/>??
  6. ????<uses-permission?android:name="android.permission.CLEAR_APP_USER_DATA"?/>??

?

?

安装程序的方法:

?

1、??通过Intent机制,调出系统安装应用,重新安装应用的话,会保留原应用的数据。

?

String fileName = Environment.getExternalStorageDirectory() +?apkName;

Uri uri = Uri.fromFile(new?File(fileName));

Intent intent =?new?Intent(Intent.ACTION_VIEW);

intent.setDataAndType(Uri, application/vnd.android.package-archive");

startActivity(intent);

?

2、??直接调用安装接口。

?

Uri mPackageURI = Uri.fromFile(new?File(Environment.getExternalStorageDirectory() +?apkName));

int?installFlags = 0;

PackageManager pm = getPackageManager();

try

{

????PackageInfo pi = pm.getPackageInfo(packageName,

????PackageManager.GET_UNINSTALLED_PACKAGES);

????if(pi !=?null)

????{

????????installFlags |= PackageManager.REPLACE_EXISTING_PACKAGE;

????}

}

catch?(NameNotFoundException e)

{}

PackageInstallObserver observer =?new?PackageInstallObserver();

pm.installPackage(mPackageURI, observer, installFlags);

?

安装应用权限:android.permission.INSTALL_PACKAGES

?

系统应用(安装在/system/app下面)可以采用该方式,第三方应用无法申请安装卸载权限。

java.lang.SecurityException: Neither user 10039 nor current process has android.permission.INSTALL_PACKAGES.

?

3、??执行install命令。

?

install –r 更新安装,默认新安装;如果不附上-r参数,则会清楚原应用的数据,版本一致则无法安装。

(1)am start …

(2)Runtime.exec(String[] args)

(3)Class<?> execClass?= Class.forName("android.os.Exec");

?

4、??执行cp / adb push命令。

?

由系统检测到应用程序有更新,自动完成重新安装。

?

5、??通过第三方软件实现。

?

Market,EOE,eTrackDog均采用第一种方法实现更新。

优点:由系统核心应用程序控制安装程序;

缺点:无法控制安装过程;安装完成后,也无法立刻启动应用,需要用户确认;无法扩展。

?

实例:Market查找安装程序

Intent intent =

new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:your.app.id"));

startActivity(intent);

?

卸载程序的方法:

?

1、??通过Intent机制,调出系统卸载应用。

Uri packageURI = Uri.parse("package: your.app.id");

Intent intent =?new?Intent(Intent.ACTION_DELETE);

startActivity(intent);

?

2、??直接调用卸载接口。

?

PackageInstallObserver?observer =?new?PackageInstallObserver();

pm.installPackage(mPackageURI, observer, installFlags);

?

卸载应用权限:android.permission.DELETE_PACKAGES

?

3、??运行rm apk安装文件,由系统检测后调用卸载应用。

  相关解决方案