意图ACTION_SEND不会显示每个浏览器 [英] Intent ACTION_SEND doesn't show every browser
问题描述
我有一个按钮,当点击以下按钮时,该按钮会触发ACTION_SEND意图:
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"));
}
当前,它具有一些其他应用程序,复制到剪贴板"和添加到Firefox",可用于在Firefox中打开链接.但是,我想让用户决定要使用哪个浏览器应用程序打开链接,但是当我正在使用的该设备同时具有"Chrome"和"Internet"应用程序时,添加到Firefox"似乎是唯一的选择
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.
最终,我想要的是一个共享按钮,并且在单击事件中,它显示所有已安装的浏览器应用程序,例如chrome,firefox,互联网"等,复制到剪贴板"以及任何其他应用程序(<-这些不是必需的.)
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.
"WEB_URL"字符串始终是正确的网址.
The 'WEB_URL' string is always going to be a proper url.
我该如何实现?
编辑
总结:
我希望有一个由"Intent.createChooser()"显示的应用程序列表,该列表包含所有浏览器应用程序和复制到剪贴板"选项.
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.
我尝试将Intent.ACTION_VIEW与intent.setData(Uri.parse(url))一起使用,但是在这种情况下,它没有复制到剪贴板"选项.
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.
推荐答案
解决方案
经过研究,我能够达到我想要的目标.
I was able to achieve what I wanted above after some research.
关键是使用"Intent.EXTRA_INITIAL_INTENTS"和自定义活动.
The key is to use 'Intent.EXTRA_INITIAL_INTENTS' and a custom Activity.
SomeActivity的onClick事件
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);
}
将"CopyToClipboardActivity"添加到清单
<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);
}
}
这篇关于意图ACTION_SEND不会显示每个浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!