android第三方应用安装来源设置分析
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);
}