问题
I have a button which fires a ACTION_SEND intent when clicked as below:
private static final String WEB_URL = "https://www.google.ca/";
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, WEB_URL);
intent.setType("text/plain");
startActivity(Intent.createChooser(intent, "CHOOOOOSE"));
}
Currently, it has some other apps, 'Copy to clipboard', and 'Add to Firefox' which can be used to open the link in Firefox. However, I want to let a user to decide which browser app to use to open the link but 'Add to Firefox' seems to be the only option right now when this device I am using has 'Chrome' and 'Internet' applications as well.
Ultimately, what I want is a share button, and on a click event, it shows all installed browser app like chrome, firefox, 'internet', etc., 'Copy to clipboard', and any other apps(<- these are not necessary, though).
Showing all browser apps, and a button to 'Copy to clipboard' is what I essentially want.
The 'WEB_URL' string is always going to be a proper url.
How do I achieve this?
EDIT
To sum up:
I want to have a list of apps shown by 'Intent.createChooser()' whose list consist of ALL browser apps AND a 'Copy to clipboard' option.
I tried using Intent.ACTION_VIEW with intent.setData(Uri.parse(url)) but then in this case, it doesn't have the 'Copy to clipboard' option.
回答1:
Solution
I was able to achieve what I wanted above after some research.
The key is to use 'Intent.EXTRA_INITIAL_INTENTS' and a custom Activity.
SomeActivity's onClick Event
private static final String WEB_URL = "https://www.google.ca/";
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(WEB_URL));
Intent clipboardIntent = new Intent(SomeActivity.this, CopyToClipboardActivity.class);
clipboardIntent.setData(Uri.parse(WEB_URL));
Intent chooserIntent = Intent.createChooser(intent, "Custom Title...");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {clipboardIntent});
startActivity(chooserIntent);
}
Add 'CopyToClipboardActivity' to Manifest
<activity android:name=".activities.CopyToClipboardActivity"
android:exported="false"
android:icon="@drawable/someIcon"
android:label="@string/copy_to_clipboard"
android:theme="@android:style/Theme.NoDisplay"/>
CopyToClipboardActivity.java
public class CopyToClipboardActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Uri uri = getIntent().getData();
if (uri != null) {
copyTextToClipboard(uri.toString());
Toast.makeText(this, "Link copied to clipboard", Toast.LENGTH_SHORT).show();
}
// Finish right away. We don't want to actually display a UI.
finish();
}
private void copyTextToClipboard(String url) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("URL", url);
clipboard.setPrimaryClip(clip);
}
}
来源:https://stackoverflow.com/questions/50515159/intent-action-send-doesnt-show-every-browser