Android靜默安裝的兩種方案

一些產(chǎn)品要求APP在升級(jí)時(shí)能夠?qū)崿F(xiàn)靜默安裝,而無需彈出安裝界面讓用戶確認(rèn)献联。這里提出兩種實(shí)現(xiàn)方案:

方案一:通過pm命令安裝

APP調(diào)用『pm』命令實(shí)現(xiàn)靜默安裝役耕,此方案無須修改Android源碼畔裕,但需要root權(quán)限。實(shí)現(xiàn)如下:

/**
 * Silent install
 *
 * @param path Package
 * @return true: success false: failed
 */
public static boolean installSilent(String path) {
    boolean result = false;
    BufferedReader es = null;
    DataOutputStream os = null;

    try {
        Process process = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(process.getOutputStream());

        String command = "pm install -r " + path + "\n";
        os.write(command.getBytes(Charset.forName("utf-8")));
        os.flush();
        os.writeBytes("exit\n");
        os.flush();

        process.waitFor();
        es = new BufferedReader(new InputStreamReader(process.getErrorStream()));

        String line;
        StringBuilder builder = new StringBuilder();
        while ((line = es.readLine()) != null) {
            builder.append(line);
        }
        Log.d(TAG, "install msg is " + builder.toString());

        /* Installation is considered a Failure if the result contains
            the Failure character, or a success if it is not.
             */
        if (!builder.toString().contains("Failure")) {
            result = true;
        }
    } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
    } finally {
        try {
            if (os != null) {
                os.close();
            }
            if (es != null) {
                es.close();
            }
        } catch (IOException e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }

    return result;
}

方案二 修改PackageInstaller源碼

如果沒有root權(quán)限景馁,方案一將無法實(shí)現(xiàn),因此我們通過定制 PackageInstaller 來實(shí)現(xiàn)指定包名可以靜默安裝逗鸣,并增加Intent參數(shù)來指定靜默安裝還是默認(rèn)安裝合住。具體修改如下:

diff --git a/packages/apps/PackageInstaller/src/com/android/packageinstaller/InstallStart.java b/packages/apps/Pac
old mode 100644
new mode 100755
index 12441b5..cbf8c41
--- a/packages/apps/PackageInstaller/src/com/android/packageinstaller/InstallStart.java
+++ b/packages/apps/PackageInstaller/src/com/android/packageinstaller/InstallStart.java
@@ -22,17 +22,30 @@ import android.app.ActivityManager;
 import android.app.AppGlobals;
 import android.app.IActivityManager;
 import android.content.ContentResolver;
+import android.content.Context;
 import android.content.Intent;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.IPackageManager;
 import android.content.pm.PackageInstaller;
 import android.content.pm.PackageManager;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageParser;
+import android.content.pm.PackageUserState;
+import android.content.pm.ProviderInfo;
 import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.RemoteException;
+import android.os.SystemProperties;
 import android.support.annotation.Nullable;
+import android.text.TextUtils;
 import android.util.Log;
+import android.content.pm.IPackageInstallObserver;
+import android.support.v4.content.FileProvider;
+
+import java.io.File;
+import java.lang.reflect.Method;
+import java.util.List;
 
 import com.android.internal.annotations.VisibleForTesting;
 
@@ -43,6 +56,8 @@ import com.android.internal.annotations.VisibleForTesting;
 public class InstallStart extends Activity {
     private static final String LOG_TAG = InstallStart.class.getSimpleName();
 
+    private static final String EXTRA_SILENT_INSTALL = "silent_install";
+
     private static final String DOWNLOADS_AUTHORITY = "downloads";
     private IActivityManager mIActivityManager;
     private IPackageManager mIPackageManager;
@@ -91,40 +106,57 @@ public class InstallStart extends Activity {
             return;
         }
 
-        Intent nextActivity = new Intent(intent);
-        nextActivity.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
-
-        // The the installation source as the nextActivity thinks this activity is the source, hence
-        // set the originating UID and sourceInfo explicitly
-        nextActivity.putExtra(PackageInstallerActivity.EXTRA_CALLING_PACKAGE, callingPackage);
-        nextActivity.putExtra(PackageInstallerActivity.EXTRA_ORIGINAL_SOURCE_INFO, sourceInfo);
-        nextActivity.putExtra(Intent.EXTRA_ORIGINATING_UID, originatingUid);
+        Uri pkgUri = intent.getData();
+        String path = "";
+        if (pkgUri != null) {
+            if (pkgUri.getScheme().equals(ContentResolver.SCHEME_FILE)) {
+                path = pkgUri.getPath();
+            } else if (pkgUri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
+                path = providerUri2Path(this, pkgUri);
+            }
+        }
 
-        if (PackageInstaller.ACTION_CONFIRM_PERMISSIONS.equals(intent.getAction())) {
-            nextActivity.setClass(this, PackageInstallerActivity.class);
+        if (isSilentInstall(intent, path)) {
+            Log.i(LOG_TAG, "silent install path: " + path);
+            getPackageManager().installPackage(Uri.fromFile(new File(path)),
+                    new PackageInstallObserver(), 2, null);
         } else {
-            Uri packageUri = intent.getData();
-
-            if (packageUri != null && (packageUri.getScheme().equals(ContentResolver.SCHEME_FILE)
-                    || packageUri.getScheme().equals(ContentResolver.SCHEME_CONTENT))) {
-                // Copy file to prevent it from being changed underneath this process
-                nextActivity.setClass(this, InstallStaging.class);
-            } else if (packageUri != null && packageUri.getScheme().equals(
-                    PackageInstallerActivity.SCHEME_PACKAGE)) {
+            Intent nextActivity = new Intent(intent);
+            nextActivity.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
+
+            // The the installation source as the nextActivity thinks this activity is the source, hence
+            // set the originating UID and sourceInfo explicitly
+            nextActivity.putExtra(PackageInstallerActivity.EXTRA_CALLING_PACKAGE, callingPackage);
+            nextActivity.putExtra(PackageInstallerActivity.EXTRA_ORIGINAL_SOURCE_INFO, sourceInfo);
+            nextActivity.putExtra(Intent.EXTRA_ORIGINATING_UID, originatingUid);
+
+            if (PackageInstaller.ACTION_CONFIRM_PERMISSIONS.equals(intent.getAction())) {
                 nextActivity.setClass(this, PackageInstallerActivity.class);
             } else {
-                Intent result = new Intent();
-                result.putExtra(Intent.EXTRA_INSTALL_RESULT,
-                        PackageManager.INSTALL_FAILED_INVALID_URI);
-                setResult(RESULT_FIRST_USER, result);
+                Uri packageUri = intent.getData();
+
+                if (packageUri != null && (packageUri.getScheme().equals(ContentResolver.SCHEME_FILE)
+                        || packageUri.getScheme().equals(ContentResolver.SCHEME_CONTENT))) {
+                    // Copy file to prevent it from being changed underneath this process
+                    nextActivity.setClass(this, InstallStaging.class);
+                } else if (packageUri != null && packageUri.getScheme().equals(
+                        PackageInstallerActivity.SCHEME_PACKAGE)) {
+                    nextActivity.setClass(this, PackageInstallerActivity.class);
+                } else {
+                    Intent result = new Intent();
+                    result.putExtra(Intent.EXTRA_INSTALL_RESULT,
+                            PackageManager.INSTALL_FAILED_INVALID_URI);
+                    setResult(RESULT_FIRST_USER, result);
 
-                nextActivity = null;
+                    nextActivity = null;
+                }
             }
-        }
 
-        if (nextActivity != null) {
-            startActivity(nextActivity);
+            if (nextActivity != null) {
+                startActivity(nextActivity);
+            }
         }
+
         finish();
     }
 
@@ -247,4 +279,94 @@ public class InstallStart extends Activity {
     void injectIActivityManager(IActivityManager iActivityManager) {
         mIActivityManager = iActivityManager;
     }
+
+    private static String providerUri2Path(Context context, Uri uri) {
+        Log.i(LOG_TAG, "providerUri2Path, uri: " + uri.toString());
+
+        try {
+            List<PackageInfo> packs = context.getPackageManager()
+                    .getInstalledPackages(PackageManager.GET_PROVIDERS);
+            if (packs != null) {
+                for (PackageInfo pack : packs) {
+                    ProviderInfo[] providers = pack.providers;
+                    if (providers != null) {
+                        for (ProviderInfo provider : providers) {
+                            if (provider.authority.equals(uri.getAuthority())) {
+                                Class<FileProvider> fileProviderClass = FileProvider.class;
+                                try {
+                                    Method getPathStrategy = fileProviderClass.getDeclaredMethod(
+                                            "getPathStrategy", Context.class, String.class);
+                                    getPathStrategy.setAccessible(true);
+                                    Object invoke = getPathStrategy.invoke(null, context, uri.getAuthority());
+                                    if (invoke != null) {
+                                        String PathStrategyStringClass = FileProvider.class.getName() + "$PathStr
+                                        Class<?> PathStrategy = Class.forName(PathStrategyStringClass);
+                                        Method getFileForUri = PathStrategy.getDeclaredMethod("getFileForUri", Ur
+                                        getFileForUri.setAccessible(true);
+                                        Object invoke1 = getFileForUri.invoke(invoke, uri);
+                                        if (invoke1 instanceof File) {
+                                            return ((File) invoke1).getAbsolutePath();
+                                        }
+                                    } else {
+                                        Log.e(LOG_TAG, "providerUri2Path, invoke is null.");
+                                    }
+                                } catch (Exception e) {
+                                    Log.e(LOG_TAG, e.getMessage());
+                                }
+                                break;
+                            }
+                        }
+                    }
+                }
+            } else {
+                Log.w(LOG_TAG, "providerUri2Path, packs is null.");
+            }
+        } catch (Exception e) {
+            Log.e(LOG_TAG, e.getMessage());
+        }
+
+        return "";
+    }
+
+    private boolean isSilentInstall(Intent intent, String path) {
+        if (!TextUtils.isEmpty(path)) {
+            if (intent.getBooleanExtra(EXTRA_SILENT_INSTALL, false)) {
+                Log.i(LOG_TAG, "isSilentInstall, Intent include EXTRA_SILENT_INSTALL.");
+                return true;
+
+            } else {
+                String value = SystemProperties.get("ro.silentinstallapps", "");
+                if (!TextUtils.isEmpty(value)) {
+                    if (TextUtils.equals(value, "all")) {
+                        Log.i(LOG_TAG, "isSilentInstall, All.");
+                        return true;
+
+                    } else {
+                        File sourceFile = new File(path);
+                        PackageParser.Package parsed = PackageUtil.getPackageInfo(this, sourceFile);
+                        if (parsed != null) {
+                            PackageInfo pkgInfo = PackageParser.generatePackageInfo(parsed, null,
+                                    PackageManager.GET_PERMISSIONS, 0, 0, null,
+                                    new PackageUserState());
+                            if (pkgInfo != null) {
+                                if (TextUtils.equals(value, "system")) {
+                                    if (TextUtils.equals(pkgInfo.sharedUserId, "android.uid.system")) {
+                                        Log.i(LOG_TAG, "isSilentInstall, System.");
+                                         return true;
+                                     }
+
+                                } else {
+                                    String[] pkgNames = value.split(",");
+                                    if (pkgNames != null && pkgNames.length > 0) {
+                                        for (String pkgName : pkgNames) {
+                                            if (TextUtils.equals(pkgName, pkgInfo.packageName)) {
+                                                Log.i(LOG_TAG, "isSilentInstall, Included in the whitelist.");
+                                                return true;
+                                            }
+                                        }
+                                    }
+                                 }
+                            }
+                        }
+                    }
+                }
+            }
+        } else {
+            Log.w(LOG_TAG, "isSilentInstall, path is null.");
+        }
+
+        return false;
+    }
+
+    class PackageInstallObserver extends IPackageInstallObserver.Stub {
+
+        @Override
+        public void packageInstalled(String packageName, int returnCode) throws RemoteException {
+            Log.i(LOG_TAG, packageName + " silent installed.");
+        }
+    }
 }

配置指定包名走靜默安裝
支持通過屬性配置需要靜默安裝的APP包名,只要是屬性配置的包名就走靜默安裝撒璧,其它APP走默認(rèn)安裝透葛。這個(gè)操作由系統(tǒng)端配置,APP端按Android標(biāo)準(zhǔn)API調(diào)應(yīng)用安裝即可卿樱。配置參考:

ro.silentinstallapps=com.ayst.sample1,com.ayst.sample1

注意 :支持同時(shí)配置多個(gè)包名僚害,包名之間用逗號(hào)隔開。
配置全部APP走靜默安裝
所有APP都走靜默安裝繁调。

ro.silentinstallapps=all

配置系統(tǒng)APP走靜默安裝
僅系統(tǒng)uid的APP走靜默安裝萨蚕,其它APP走默認(rèn)安裝靶草。

ro.silentinstallapps=system

指定Intent參數(shù)走靜默安裝
通過Intent參數(shù)指定是否要靜默安裝。使用方法如下:

intent.putExtra("silent_install", true); // 靜默安裝

完整參考:

private static final String EXTRA_SILENT_INSTALL = "silent_install";

public static void install(Context context, String path) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        installO(context, path);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        installN(context, path);
    } else {
        installOther(context, path);
    }
}

/**
 * android1.x-6.x
 *
 * @param context Context
 * @param path    Package
 */
private static void installOther(Context context, String path) {
    Intent install = new Intent(Intent.ACTION_VIEW);
    install.setDataAndType(Uri.parse("file://" + path),
                           "application/vnd.android.package-archive");
    install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    install.putExtra(EXTRA_SILENT_INSTALL, true); // 靜默安裝
    context.startActivity(install);
}

/**
 * android7.x
 *
 * @param context Context
 * @param path    Package
 */
private static void installN(Context context, String path) {
    Uri apkUri = FileProvider.getUriForFile(context, AUTHORITY, new File(path));
    Intent install = new Intent(Intent.ACTION_VIEW);
    install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    install.setDataAndType(apkUri, "application/vnd.android.package-archive");
    install.putExtra(EXTRA_SILENT_INSTALL, true); // 靜默安裝
    context.startActivity(install);
}

/**
 * android8.x
 *
 * @param context Context
 * @param path    Package
 */
@RequiresApi(api = Build.VERSION_CODES.O)
private static void installO(Context context, String path) {
    boolean isGranted = context.getPackageManager().canRequestPackageInstalls();
    if (isGranted) {
        installN(context, path);
    } else {
        Dialog dialog = new AlertDialog.Builder(context.getApplicationContext())
            .setTitle("Unknown sources")
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface d, int w) {
                    Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
                    context.startActivity(intent);
                }
            }).create();
        dialog.setCancelable(false);
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        dialog.show();
    }
}

https://www.yuque.com/aiyinsitan-dhjkq/android-system/fngm5h

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末岳遥,一起剝皮案震驚了整個(gè)濱河市爱致,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌寒随,老刑警劉巖糠悯,帶你破解...
    沈念sama閱讀 218,858評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異妻往,居然都是意外死亡互艾,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門讯泣,熙熙樓的掌柜王于貴愁眉苦臉地迎上來纫普,“玉大人,你說我怎么就攤上這事好渠∽蚣冢” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵拳锚,是天一觀的道長假栓。 經(jīng)常有香客問我,道長霍掺,這世上最難降的妖魔是什么匾荆? 我笑而不...
    開封第一講書人閱讀 58,842評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮杆烁,結(jié)果婚禮上牙丽,老公的妹妹穿的比我還像新娘。我一直安慰自己兔魂,他們只是感情好烤芦,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著析校,像睡著了一般构罗。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上勺良,一...
    開封第一講書人閱讀 51,679評(píng)論 1 305
  • 那天绰播,我揣著相機(jī)與錄音,去河邊找鬼尚困。 笑死蠢箩,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播谬泌,決...
    沈念sama閱讀 40,406評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼滔韵,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了掌实?” 一聲冷哼從身側(cè)響起陪蜻,我...
    開封第一講書人閱讀 39,311評(píng)論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎贱鼻,沒想到半個(gè)月后宴卖,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,767評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡邻悬,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年症昏,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片父丰。...
    茶點(diǎn)故事閱讀 40,090評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡肝谭,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蛾扇,到底是詐尸還是另有隱情攘烛,我是刑警寧澤,帶...
    沈念sama閱讀 35,785評(píng)論 5 346
  • 正文 年R本政府宣布镀首,位于F島的核電站坟漱,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏蘑斧。R本人自食惡果不足惜靖秩,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評(píng)論 3 331
  • 文/蒙蒙 一须眷、第九天 我趴在偏房一處隱蔽的房頂上張望竖瘾。 院中可真熱鬧,春花似錦花颗、人聲如沸捕传。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽庸论。三九已至,卻和暖如春棒呛,著一層夾襖步出監(jiān)牢的瞬間聂示,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評(píng)論 1 271
  • 我被黑心中介騙來泰國打工簇秒, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留鱼喉,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,298評(píng)論 3 372
  • 正文 我出身青樓,卻偏偏與公主長得像扛禽,于是被迫代替她去往敵國和親锋边。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容