一、通過Intent訊息機制發送訊息,調用系統應用進行,實現apk的安裝/卸載 。
(1) 調用系統的安裝應用,讓系統自動進行apk的安裝
String fileName = "/data/data/com.zlc.ipanel.operate/FileOperate.apk";
Uri uri = Uri.fromFile(new File(fileName));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
上訴安裝不僅可以安裝新的apk(從無到有),也可以用於更新舊的apk(版本更新),在進行版本更新的時候,必須保證兩個apk的簽名是一致的。
如果是一般應用,安裝到data/分區下面,新的apk會替換舊的apk。
如果是系統應用,一般安裝到system/app下面,更新之後, system/app/下的舊apk仍然存在,系統會將新的apk被複製到data/app下。雖然同時存在兩新舊apk,但運行時系統選擇data分區的新apk運行, 如果執行卸載則是刪除data分區的apk,再次啟動程式啟動並執行是system目錄下舊的apk。
(2)啟動系統的卸載應用,讓系統自動卸載apk
//通過程式的包名建立URI
Uri packageURI = Uri.parse("package:com.zlc.ipanel");
//建立Intent意圖
Intent intent = new Intent(Intent.ACTION_DELETE,packageURI);
//執行卸載程式
startActivity(intent);
與apk安裝不同的是,Intent訊息這裡改了:ACTION_DELETE,apk安裝使用的是(ACTION_VIEW)
二、通過調用系統提供的介面packageManager對apk進行卸載、安裝、擷取許可權等(靜默安裝)
(1)需要平台簽名(寫個Android.mk在源碼下編譯最省事,當然網上也有很多其他方法,比如:singapk命令列簽名)
(2)由於調用的是未公開的API,所以需要引入源碼(僅僅做一個馬甲,不參與編譯)
(3)在AndroidManifest.xml添加安裝apk的許可權
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
(4) 如果升級失敗出現 Unable to open zip '/data/FileOperate.apk': Permission denied,說明是許可權不夠,有可能當前apk只有(-rw-------)許可權,通過Runtime.exec方法修改許可權之後再進行升級是可以成功 的。
PackageManager pm = this.getPackageManager();
File file = new File("/data/data/com.zlc.ipanel.operate/FileOperate.apk");
pm.installPackage(Uri.fromFile(file), null,
PackageManager.INSTALL_REPLACE_EXISTING, "com.zlc.ipanel");
(5)下面舉一個例子通過Eclipse來實現靜默安裝/卸載。
1)按照我們上面講的靜默安裝需求一步步實現我們的操作。首先準備馬甲(靜默安裝需要調用的介面)
由於調用了系統未公開的介面,而這些介面有些是通過aidl實現的,下面我們把需要的馬甲修改一下。
PackageManager.java
/*
* Copyright (C) 2006 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 android.content.pm;
import android.content.Context;
import android.net.Uri;
/**
* Class for retrieving various kinds of information related to the application
* packages that are currently installed on the device.
*
* You can find this class through {@link Context#getPackageManager}.
*/
public abstract class PackageManager {
/**
* Flag parameter for {@link #installPackage} to indicate that you want to replace an already
* installed package, if one exists.
* @hide
*/
public static final int INSTALL_REPLACE_EXISTING = 0x00000002;
/**
* @hide
*
* Install a package. Since this may take a little while, the result will
* be posted back to the given observer. An installation will fail if the calling context
* lacks the {@link android.Manifest.permission#INSTALL_PACKAGES} permission, if the
* package named in the package file's manifest is already installed, or if there's no space
* available on the device.
*
* @param packageURI The location of the package file to install. This can be a 'file:' or a
* 'content:' URI.
* @param observer An observer callback to get notified when the package installation is
* complete. {@link IPackageInstallObserver#packageInstalled(String, int)} will be
* called when that happens. observer may be null to indicate that no callback is desired.
* @param flags - possible values: {@link #INSTALL_FORWARD_LOCK},
* {@link #INSTALL_REPLACE_EXISTING}, {@link #INSTALL_ALLOW_TEST}.
* @param installerPackageName Optional package name of the application that is performing the
* installation. This identifies which market the package came from.
*/
public abstract void installPackage(
Uri packageURI, IPackageInstallObserver observer, int flags,
String installerPackageName);
/**
* Attempts to delete a package. Since this may take a little while, the result will
* be posted back to the given observer. A deletion will fail if the calling context
* lacks the {@link android.Manifest.permission#DELETE_PACKAGES} permission, if the
* named package cannot be found, or if the named package is a "system package".
* (TODO: include pointer to documentation on "system packages")
*
* @param packageName The name of the package to delete
* @param observer An observer callback to get notified when the package deletion is
* complete. {@link android.content.pm.IPackageDeleteObserver#packageDeleted(boolean)} will be
* called when that happens. observer may be null to indicate that no callback is desired.
* @param flags - possible values: {@link #DONT_DELETE_DATA}
*
* @hide
*/
public abstract void deletePackage(
String packageName, IPackageDeleteObserver observer, int flags);
}
安裝成功的回調介面IPackageInstallObserver.java(修改過的馬甲)
package android.content.pm;
import android.os.RemoteException;
public interface IPackageInstallObserver {
public class Stub implements IPackageInstallObserver{
public void packageInstalled(String packageName, int returnCode)
throws RemoteException {
// TODO Auto-generated method stub
}
}
}
卸載成功的回調介面IPackageDeleteObserver.java(修改過的馬甲)
package android.content.pm;
public interface IPackageDeleteObserver {
public class Stub implements IPackageDeleteObserver{
public void packageDeleted(String packageName, int returnCode) {
// TODO Auto-generated method stub
}
}
}
2)馬甲準備好之後,就開始實現我們的操作邏輯。
實現靜默安裝/卸載的方法,並且註冊回調(無論失敗成功都會返回對應值)
//靜默安裝
public static void installApkDefaul(Context context,String fileName,String pakcageName){
Log.d(TAG, "jing mo an zhuang");
File file = new File(fileName);
int installFlags = 0;
if(!file.exists())
return;
Log.d(TAG, "jing mo an zhuang out");
installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
PackageManager pm = context.getPackageManager();
IPackageInstallObserver observer = new MyPakcageInstallObserver();
pm.installPackage(Uri.fromFile(file), observer, installFlags, pakcageName);
}
//靜默卸載
public static void uninstallApkDefaul(Context context,String packageName){
PackageManager pm = context.getPackageManager();
IPackageDeleteObserver observer = new MyPackageDeleteObserver();
pm.deletePackage(packageName, observer, 0);
}
//靜默卸載回調
private static class MyPackageDeleteObserver extends IPackageDeleteObserver.Stub{
@Override
public void packageDeleted(String packageName, int returnCode) {
// TODO Auto-generated method stub
Log.d(TAG, "returnCode = "+returnCode);//返回1代表卸載成功
}
}
//靜默安裝回調
private static class MyPakcageInstallObserver extends IPackageInstallObserver.Stub{
@Override
public void packageInstalled(String packageName, int returnCode)
throws RemoteException {
// TODO Auto-generated method stub
Log.i(TAG, "returnCode = " + returnCode);//返回1代表安裝成功
}
}
3)主要的邏輯實現之後,在AndroidManifest.xml 添加安裝和卸載的許可權
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" />
4)運行Eclipse,產生該應用的apk,下面就要進行系統簽名了。寫一個指令碼來來實現替換成系統簽名
使用簽名包簽名首先我們要去源碼裡面取出下面這幾個檔案platform.x509.pem、platform.pk8、signapk.jar
指令碼1.bat (和產生的apk以及上面三個檔案放到同一個目錄下面,一般為工程下面的bin目錄)
java -jar signapk.jar platform.x509.pem platform.pk8 FileUpdateControl.apk 1.apk
adb uninstall com.zlc.ipanel.operate
adb install 1.apk
5)當下載到系統裡面的apk許可權不夠時(靜默安裝提示許可權問題,有可能當前apk只有(-rw-------)許可權)
可以使用下面三種方式修改許可權
1.使用Runtime來啟動一個線程修改許可權
try {
Runtime.getRuntime().exec("chmod 777 " + file.getCanonicalPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2.寫一個native介面直接通過jni調用c的介面修改許可權。
3.使用FileUtils,這個類預設是隱藏的,官方sdk中不包含這個類,所以代碼如果要使用這個類,需要將工程放到android源碼中編譯
FileUtils.setPermissions(f.getAbsolutePath(), FileUtils.S_IRUSR | FileUtils.S_IWUSR | FileUtils.S_IRGRP | FileUtils.S_IROTH, -1, -1) ;
三、其他apk安裝的方法
(1)通過cmd視窗 adb 命令,install / uninstall apk。
(2)cp apk 到 system/app目錄下,系統會自動安裝
(3)直接通過代碼調用pm命令執行apk的安裝和卸載
1 |
execCommand("pm", "install", "-f", filePath);//安裝apk,filePath為apk檔案路徑,如/sdcard/operate.apk |
2 |
execCommand("pm", "uninstall", packageName);//卸載apk,packageName為包名,如com.zlc.ipanel |