问题
I have a bunch of Android devices which are to be flashed with custom ROMs and given out to clients. As part of that ROM will be a 'support' app, which is tied to the device. It can't be published to Google Play. I need to be able to offer users the opportunity to download and install updated versions of the software. I have checking, download and install code already implemented however it relies on the devices being configured to enable installation of apps from unknown sources. I need the device to be able to download and install this particular apk, whilst still not allowing any other apps from unknown sources to be installed.
Is this possible?
(edit: to clarify how the chosen answer finally worked)
The code added to the activity was this:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() +"/update.apk")), "application/vnd.android.package-archive");
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.putExtra(Intent.EXTRA_ALLOW_REPLACE, true);
startActivityForResult(intent, 0);
To the android manifest, the following code was added:
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
After installing the app, I used a root file explorer to move the apk from /user/apps
to /system/apps
then after resetting the phone the app was able to install itself, over the top of itself, without being prompted for the user to enable untrusted sources. The install prompt, listing the permissions the app requires and giving the user the choice to install or not still appears, but that is fine.
回答1:
This method only works for system apps (source)
Try installing the apk by adding and extra field.
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
Have never tried this. The doc says you need to call startActivityforResult for this to work. But this requires API level 14. Also try
intent.putExtra(Intent.EXTRA_ALLOW_REPLACE, true);
Edit:
On second thought, if you have custom ROM, then add shareduserid system to your installer app and directly call
whatever PackageInstallerActivity
is calling to install the app
Edit2:
Check this code. OnClick is used to install app, so copy the whole code to your app and add
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
permission.
And to App's Android.mk add
LOCAL_CERTIFICATE := platform
来源:https://stackoverflow.com/questions/12263928/how-to-push-updates-to-preinstalled-apps-without-allowing-installation-from-unkn