Android Instant App: showInstallPrompt's postI

2019-01-28 07:55发布

问题:

The app in the play store currently is configured to handle https://www.example.com/hello/in an intent filter to launch an activity and showInstallPrompt(...) is supposed to launch the activity once installation of the app is finished.

https://developer.android.com/topic/instant-apps/reference.html#public_methods

showInstallPrompt(...) Docs:

Shows a dialog that allows the user to install the current instant app. This method is a no-op if the current running process is an installed app. You must provide a post-install intent, which the system uses to start the application after install is complete.

postInstallIntent Docs:

The intent to launch after the instant app has been installed. This intent must resolve to an activity in the installed app package, or it will not be used.

I've tried doing this:

Uri uri = Uri.parse("https://www.example.com/hello/");
Intent postInstallIntent = new Intent("action", uri);
InstantApps.showInstallPrompt(MainActivity.this, postInstallIntent, 0, "InstantApp");

and this

Intent postInstallIntent = new Intent("https://www.example.com/hello/");
InstantApps.showInstallPrompt(MainActivity.this, postInstallIntent, 0, "InstantApp");

and this

Intent postInstallIntent = new Intent(Intent.ACTION_VIEW,
        Uri.parse("https://www.example.com/hello/"))
        .addCategory(Intent.CATEGORY_DEFAULT)
        .addCategory(Intent.CATEGORY_BROWSABLE);

It brings me the the play store, but neither launches the app automatically once the install is completed.

回答1:

Please have a look at:
https://github.com/googlesamples/android-instant-apps/tree/master/install-api
and
https://github.com/googlesamples/android-instant-apps/blob/master/install-api/features/install/src/main/java/com/instantappsamples/feature/install/InstallApiActivity.kt

  • This sample app demonstrates how to use the Install API. The API triggers the Intent to install the app on device.
  • The call also accepts the Intent, which is triggered after the installation is complete.
  • The sample also shows the correct structure to implement showInstallPrompt method along with postInstallIntent.

Refer the sample code snippet:

private val postInstallIntent = Intent(Intent.ACTION_VIEW,
        Uri.parse("https://install-api.instantappsample.com/")).
        addCategory(Intent.CATEGORY_BROWSABLE).
        putExtras(Bundle().apply {
            putString("The key to", "sending data via intent")
        })

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_install)

    val isInstantApp = InstantApps.isInstantApp(this)

    findViewById<Button>(R.id.start_installation).apply {
        isEnabled = isInstantApp
        // Show the installation prompt only for an instant app.
        if (isInstantApp) {
            setOnClickListener {
                InstantApps.showInstallPrompt(this@InstallApiActivity,
                        postInstallIntent,
                        REQUEST_CODE,
                        REFERRER)
            } }
    } }