Android - download another app programmatically - android

Is it possible to download apps programmatically? I don't mean to launch Google Play to a certain app. Let's say i have 3 apps, all related but have different functionality. Is it possible to create a 4th app which when you download also downloads the other 3 and serves as a hub for them? If so, can someone point me in the right direction?

Yep. I just did this the other day. Your app will need some permissions:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.DELETE_PACKAGES"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Write external storage is because you'll have to download and save your APK file somewhere.
Additionally, you'll need to grab the file somehow. I use Java's HttpUrlConnection object to do the actual downloading (you can probably search for a better explanation of how to download a file in an Android app, but it's pretty straightforward).
Once you have your file downloaded, you just start up the package manager and tell it to install. Unfortunately, the user will be faced with the same dialog as usual for installing an app, and they can choose to decline the installation. To install without user intervention, you would probably need to connect from your app to ADB and call the package manager that way, although I'm not sure what kind of privileges would be involved there. If you don't mind the user seeing an install confirmation dialog, then this code will work:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(new File("/sdcard/whereever-you-saved-this.apk")), "application/vnd.android.package-archive");
startActivityForResult(intent, 0);
The FLAG_ACTIVITY_NEW_TASK prevents crashes on some phones if the APK you're installing overwrites the app you used to launch the Package Manager.

You can't automatically install apps, this would be pretty risky for users.
You could download a bunch of new APKs, but the user would have to install them by himself by opening the .apks.
You can however also create 1 big app internally, but show it as multiple small apps since you can set multiple Activities as launcher activity. Maybe that's an option for you?

There is also the option for Expansion Files. Perhaps that might fit the bill?

You can do this in this way also:
Download APKs files from server via web-service and after download install these APK files by using INSTALL_PACKAGES permission.

Related

How to push updates to preinstalled apps without allowing installation from unknown sources

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.
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

How to grant system permissions to my android app?

I'm implementing a demo on silently uninstall an app from device.
In adb shell, I can use pm uninstall packagename to do the task, but when I wrote code, I got some permission denied error.
I've been googling for a while, and found that to get DELETE_PACKAGES permission, I have to sign my app with the same certificate as the system does.
So, can anyone give me some tips on how to do this? Or, is there anyway I can do to make my app running as system service?
You can't do this, unless you are building your own firmware (ROM). If you are, just take the key that signs the ROM and sign your apk with it. If you have a rooted phone, you can also copy the apk in /system/app to get the permission.
Far better than a silent uninstall is somehow bricking the app. There's a few ways that this could be done, but basically keep track of the first day they used it, and make the program not work. Alternatively, it could be set up to work until a certain day, after which it will no longer work. This question answers how to do this.
If your application is not located at "/system/app",permission "DELETE_PACKAGES" would not work.
Compile your app with source code or try "root" ;)
I have experience about how get one application permission to read browser bookmark
Open the AndroidManifest.xml of That application that you want to add permission to it.
2.Somewhere between
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
</manifest>
add this code
<uses-permission android:name="..." />
3.For fill the ... go here
For example the following code will permit to app to get bookmark history

How to update apk through code

My application is already installed in the devise and i will get notification that you want to update, at one click i want my application get updated programmatically. Is there anyway to do...
Third party developers are not able to install .apk files directly without user intervention.
But you can send request to PackageManager to handle installation of new .apk file. This will show "install the app" dialog to user, and if she accepts the installation, your update will go through.
An example of the described scenario: Android: install .apk programmatically
The only way i'm seeing of doing that, and its NOT a good policy it to have the initial code swapping a specific directory for classes, and if it founds one, load it by reflection on the fly and "replacing" the classe contained in the APK
I don't think this will have a good performance... and should not i repeat NOT be a policy for update.

How to prevent a user from installing applications which uses some specific permissions

What is the best way to prevent a user from downloading and installing applications which uses some specific permissions like location and sms.
Is it programatically possible to parse the manifest of an application from the market before it get installed and look for the specific permissions and alert the user?
What is the best way to prevent a user from downloading and installing applications which uses some specific permissions like location and sms.
Write your own firmware, where you replace the normal installer mechanism with one that enforces your desired criteria.
Android SDK applications cannot interfere with application installation.
Is it programatically possible to parse the manifest of an application from the market before it get installed and look for the specific permissions and alert the user?
No, sorry.
However, you can listen for ACTION_PACKAGE_ADDED and examine the newly-installed package via PackageManager, and alert the user at that point. Since nothing of the installed package can run immediately upon the install, I would think that there is a decent chance that your alert will appear before the user tries to use the newly-installed app.
In the future this would be probably something you could do trough Device Administration, but right now limiting application installation based on its requested permission is not included.
One option is this snippet that decompress the apk and extracts the AndroidManifest.xml.
By the way the aapt tool for android runs on the Android OS too. You can get this information using this port

Android APK's Silent Installation

I'm searching for a way to program my application to install silently an APK file.
I'm aware about the possibility to launch with code that looks something like this:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
startActivity(intent);
But before the installation starts, this code raises a dialog with the APK's required permissions, and needs user authorization to start the installation.
Is there any way to skip this dialog?
Is there any other way to install an application during runtime from my code, that doesn't require user interaction?
No. And that's a good thing - this would be an (other) open door to malware and unwanted installs. Why do you want to do that, if you mind me asking? What's wrong with letting users know that you want to install something on their device?
Also, some details here: Silent installation on Android devices
And finally, this might be possible for rooted devices: if you write your own installer, you can bypass completely the built-in installer, and with root privilege, you can basically do what you want. But I still think that would be a serious breach of security.
Yes you can, but you need root access or your app must be a system signed app.
You can install apps silently by using shell commmand pm install "apk path".
This will definitely work - I have already created a sample app that does this.

Categories

Resources