歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android第三方應用安裝來源設置分析

Android第三方應用安裝來源設置分析

日期:2017/3/1 11:06:58   编辑:Linux編程
1.在系統設置裡面->應用程序設置->未知來源設置
下面是界面元素定義
\packages\apps\Settings\res\xml\application_settings.xml
<CheckBoxPreference
Android:key="toggle_install_applications"
android:title="@string/install_applications"
android:summaryOff="@string/install_unknown_applications"
android:summaryOn="@string/install_unknown_applications"
android:persistent="false" />

下面是界面設置變動修改的settings信息。1是允許 0是不允許
// Change the system setting
Settings.Secure.putInt(getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS,
enabled ? 1 : 0);
只是修改了Settings.Secure.INSTALL_NON_MARKET_APPS這個設置變量並沒有做其它的事情;


2.在系統安裝apk時會調用系統裡面的一個叫PackageInstaller.apk
packages\apps\PackageInstaller\src\com\android\packageinstaller\PackageInstallerActivity.java
1)安裝apk時會調用這個系統的apk執行安裝過程,在創建這個安裝的activity時onCreate()會去check未知來源設置
//check setting
if(!isInstallingUnknownAppsAllowed()) {
//ask user to enable setting first
showDialogInner(DLG_UNKNOWN_APPS);
return;
}
如果你設置了不允許安裝,會彈出禁止安裝的Dialog,點擊設置會把你引導到系統設置->應用程序設置裡面。
有個問題是,只要設置為不允許,調用PackageInstaller.apk安裝都會彈出,不知道如何做到可以不彈出?現在開發的版本大多都是定制的。找遍幾個手機,都沒有發現,googleappstore安裝不需要設置的。個人認為如果要修改,可能就會在這裡面做文章,而不是修改PackageManagerService.
2)調用初始化安裝,再調 startInstallConfirm->InstallAppProgress.java啟動安裝進度

// Start subactivity to actually install the application
Intent newIntent = new Intent();
newIntent.putExtra(PackageUtil.INTENT_ATTR_APPLICATION_INFO,
mPkgInfo.applicationInfo);
newIntent.setData(mPackageURI);
newIntent.setClass(this, InstallAppProgress.class);
String installerPackageName = getIntent().getStringExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME);
if (installerPackageName != null) {
newIntent.putExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME, installerPackageName);
}
if(localLOGV) Log.i(TAG, "downloaded app uri="+mPackageURI);

startActivity(newIntent);

調用AndroidManifest.xml安裝和卸載


在AndroidManifest.xml的源碼中我們知道:
packages\apps\PackageInstaller\AndroidManifest.xml
<activity android:name=".PackageInstallerActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="@style/TallTitleBarTheme">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="content" />
<data android:scheme="file" />
<data android:mimeType="application/vnd.android.package-archive" />
</intent-filter>
</activity>


<activity android:name=".UninstallerActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="@style/TallTitleBarTheme">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.DELETE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="package" />
</intent-filter>
</activity>


安裝一個程序:


String fileName = Environment.getExternalStorageDirectory() + "/myApp.apk";
Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setDataAndType(Uri.parse("file://" + filePath),"application/vnd.android.package-archive");
//或者
//intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");

startActivity(intent);

卸載一個程序:
Uri packageURI = Uri.parse("package:com.android.myapp");
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);


程序的安裝請注意:默認是不支持安裝非市場程序的 因此判斷一下
int result = Settings.Secure.getInt(getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS, 0);
if (result == 0) {
// show some dialog here
// ...
// and may be show application settings dialog manually
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_SETTINGS);
startActivity(intent);
}

Copyright © Linux教程網 All Rights Reserved