0

I have a question that I see a few answers for on stack overflow, but to no avail. What im trying to do is this:

I am making an application that checks if an app is installed first. For example, com.appA.android.

If appA is not installed, it prompts the user with prompt install to install the app before continuing. Im not trying to install APKs without user permission, but with their permission. appA is in the store, but i dont want my users to be pulled away from the application. Currently, my way of doing this looks like:

Intent promptInstall = new Intent(Intent.ACTION_VIEW)
                      .setDataAndType(Uri.parse("file:///path/to/apk"),
               "application/vnd.android.package-archive");
               startActivity(promptInstall);

any way i can do this with a market link?

Thanks for any help!

DirectXgameR
  • 121
  • 1
  • 8
  • No, you cannot download an apk from the Play Store by yourself, you have to go through the store's UI, either the on-device app or the web interface. – Chris Stratton Mar 13 '15 at 14:41

2 Answers2

1

For security Reasons you can't install APK without an installation Dialog or within the Background. You can install APK's in the background with root and the Packagemanager.

There are several attempts to listen for the installation of the app, so you can restart you activity. One is registering a BroadcastReceiver, which listen for an Installation of the App.

<receiver android:name=".PackageReceiver">
<intent-filter>
    <action android:name="android.intent.action.PACKAGE_ADDED" />
    <action android:name="android.intent.action.PACKAGE_CHANGED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="package" />
</intent-filter>

This class then gets called when a new package is installed:

 public class PackageReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {
 // handle install event here
 }
}

A second attempt is having a Timer which try to find the app all nSeconds for installation. Sample from another Question:

public class Example extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //Put the package name here...
        boolean installed  =   appInstalledOrNot("com.Ch.Example.pack");  
        if(installed) {
            //This intent will help you to launch if the package is already installed
            Intent LaunchIntent = getPackageManager()
                .getLaunchIntentForPackage("com.Ch.Example.pack");
            startActivity(LaunchIntent);

            System.out.println("App already installed on your phone");        
        }
        else {
            System.out.println("App is not installed on your phone");
        }
    }

    private boolean appInstalledOrNot(String uri) {
        PackageManager pm = getPackageManager();
        boolean app_installed = false;
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            app_installed = true;
        }
        catch (PackageManager.NameNotFoundException e) {
            app_installed = false;
        }
        return app_installed ;
    }
}

Last but not least: If you have the APK and root, you can install it using the shell command:

pm install APKFile.apk
Emanuel
  • 8,027
  • 2
  • 37
  • 56
  • Please re-read the question with more care as your post does not address the actual question which was asked. – Chris Stratton Mar 13 '15 at 14:39
  • Sure, it does. Because it will let the user stay in the current activity since the current Activity can listen for the installation and resume it. – Emanuel Mar 13 '15 at 14:40
  • No, it does not, because you overlook the fact that **you cannot obtain the apk file form the play store to do this with**. That, not the method of what to do if you could, is the core of the question. – Chris Stratton Mar 13 '15 at 14:41
0

Try something like this, which will open the Google Play details page for a specific app. (Presuming this is what you want to do)

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.example.android"));
startActivity(intent);
Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
  • Thanks for your response. However, im trying to keep my users inside of the app and be greeted with the package manager "install/market/cancel" prompt rather than be pulled away. – DirectXgameR Mar 13 '15 at 14:38
  • AFAIK, you cannot install an app directly from the market in the background without showing the market UI. You might try to programatically download the APK from the market, and install that in the background. – Jonas Czech Mar 13 '15 at 14:41
  • I would be perfectly ok with downloading the file to storage first, than prompting for install. What's the best way to do this? Thanks! – DirectXgameR Mar 13 '15 at 14:42
  • @DirectXgameR [See this answer](http://stackoverflow.com/a/13704021/4428462).There is also an [APK Downloader](http://apps.evozi.com/apk-downloader/) website, which you might be able to scrape. However, you should **not** do this, as the user might not have 'install apps from unknown sources' enabled in settings, and the installed app won't get updates via the play store, and a number of other problems. Sending the user to the market do download the app should not be so bad, should it? – Jonas Czech Mar 13 '15 at 14:48