1

I use this code to share text from my application:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, GetTxt());
intent.setType("text/plain");
startActivityForResult(intent, REQUEST_SEND_MSG);

and it appear the activity where I can choose the application on which I want to share. I noticed if I click on "Always open with..." I can't change it.

So how do I force the user to always select for application? On api 29 "PackageManager.clearPackagePreferredActivities" is deprecated so what can I use?

img.simone
  • 632
  • 6
  • 10
  • 23
  • I'm not sure what are you asking, so you can read about https://developer.android.com/reference/android/content/Intent#setPackage(java.lang.String) or https://developer.android.com/reference/android/content/Intent#ACTION_CHOOSER – Admir Sep 19 '19 at 16:04

2 Answers2

1

You can try with an intent chooser like this:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, GetTxt());
intent.setType("text/plain");
startActivityForResult(intent, REQUEST_SEND_MSG);

    Intent openInChooser = Intent.createChooser(intent, "Share");
    startActivityForResult(openInChooser, REQUEST_SEND_MSG);

It's generic, so you can share your text with what you want

Simone
  • 311
  • 4
  • 16
0

Your app cannot clear the preferred activities from other apps that are set by the user as default activities. Up until API 28 you could use clearPackagePreferredActivities to unset the default activities if they belonged to your app, but now with API 29 this is deprecated.

This method was deprecated in API level 29. This function no longer does anything. It is the platform's responsibility to assign preferred activities and this cannot be modified directly. To determine the activities resolved by the platform, use resolveActivity(Intent, int) or queryIntentActivities(Intent, int).

Instead, it's suggested your app queries for role availability with Role Manager.

To configure an app to be responsible for a particular role and to check current role holders, see RoleManager.

That said, there is also this answer with a hack method of achieving that, but I have no clue if it still works or not. Also, I imagine if it works, it would only work the first time.

Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39