DirectXgameR
DirectXgameR

Reputation: 131

Installing APK from market with prompt install from within application

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!

Upvotes: 0

Views: 1458

Answers (2)

Emanuel
Emanuel

Reputation: 8106

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

Upvotes: 1

Jonas Czech
Jonas Czech

Reputation: 12328

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);

Upvotes: 0

Related Questions