Example usage for android.content Intent EXTRA_ALLOW_REPLACE

List of usage examples for android.content Intent EXTRA_ALLOW_REPLACE

Introduction

In this page you can find the example usage for android.content Intent EXTRA_ALLOW_REPLACE.

Prototype

String EXTRA_ALLOW_REPLACE

To view the source code for android.content Intent EXTRA_ALLOW_REPLACE.

Click Source Link

Document

Used as a boolean extra field with #ACTION_INSTALL_PACKAGE to install a package.

Usage

From source file:org.fdroid.fdroid.installer.DefaultInstallerActivity.java

@SuppressLint("InlinedApi")
private void installPackage(Uri uri) {
    if (uri == null) {
        throw new RuntimeException("Set the data uri to point to an apk location!");
    }// w  w  w .j av a  2 s.c o m
    // https://code.google.com/p/android/issues/detail?id=205827
    if ((Build.VERSION.SDK_INT < 24) && (!uri.getScheme().equals("file"))) {
        throw new RuntimeException("PackageInstaller < Android N only supports file scheme!");
    }
    if ((Build.VERSION.SDK_INT >= 24) && (!uri.getScheme().equals("content"))) {
        throw new RuntimeException("PackageInstaller >= Android N only supports content scheme!");
    }

    Intent intent = new Intent();

    // Note regarding EXTRA_NOT_UNKNOWN_SOURCE:
    // works only when being installed as system-app
    // https://code.google.com/p/android/issues/detail?id=42253

    if (Build.VERSION.SDK_INT < 14) {
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
    } else if (Build.VERSION.SDK_INT < 16) {
        intent.setAction(Intent.ACTION_INSTALL_PACKAGE);
        intent.setData(uri);
        intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
        intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
        intent.putExtra(Intent.EXTRA_ALLOW_REPLACE, true);
    } else if (Build.VERSION.SDK_INT < 24) {
        intent.setAction(Intent.ACTION_INSTALL_PACKAGE);
        intent.setData(uri);
        intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
        intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
    } else { // Android N
        intent.setAction(Intent.ACTION_INSTALL_PACKAGE);
        intent.setData(uri);
        // grant READ permission for this content Uri
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
        intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
    }

    try {
        startActivityForResult(intent, REQUEST_CODE_INSTALL);
    } catch (ActivityNotFoundException e) {
        Log.e(TAG, "ActivityNotFoundException", e);
        installer.sendBroadcastInstall(downloadUri, Installer.ACTION_INSTALL_INTERRUPTED,
                "This Android rom does not support ACTION_INSTALL_PACKAGE!");
        finish();
    }
    installer.sendBroadcastInstall(downloadUri, Installer.ACTION_INSTALL_STARTED);
}