Android “如何才能做到?”;通过“共享”;除我的应用程序列表之外的其他内容
我正在开发一个基于文本的应用程序,它可以通过在共享中与其他应用程序共享某些部分,还可以接受其他应用程序的共享文本。当我想共享我的数据时,我的应用程序出现在“共享通过”屏幕中,如何从该屏幕中删除我的应用程序,尽管我想在其他应用程序共享一些文本时,在“共享通过”屏幕中,我的应用程序应放在那里。这是我的密码:Android “如何才能做到?”;通过“共享”;除我的应用程序列表之外的其他内容,android,android-sharing,Android,Android Sharing,我正在开发一个基于文本的应用程序,它可以通过在共享中与其他应用程序共享某些部分,还可以接受其他应用程序的共享文本。当我想共享我的数据时,我的应用程序出现在“共享通过”屏幕中,如何从该屏幕中删除我的应用程序,尽管我想在其他应用程序共享一些文本时,在“共享通过”屏幕中,我的应用程序应放在那里。这是我的密码: Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); sh
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "Here is the share content";
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
而且:
<activity
android:name=".TestActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
谢谢。找到了一种创建自定义选择器的方法,可以阻止某些应用程序显示 只需在字符串数组黑名单中传递您的软件包名称,您的应用程序将被丢弃。
String[] nameOfAppsToShareWith = new String[] { "facebook", "twitter", "gmail" };
String[] blacklist = new String[]{"com.any.package", "net.other.package"};
// your share intent
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "some text");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "a subject");
// ... anything else you want to add invoke custom chooser
startActivity(generateCustomChooserIntent(intent, blacklist));
private Intent generateCustomChooserIntent(Intent prototype,
String[] forbiddenChoices)
{
List<Intent> targetedShareIntents = new ArrayList<Intent>();
List<HashMap<String, String>> intentMetaInfo = new ArrayList<HashMap<String, String>>();
Intent chooserIntent;
Intent dummy = new Intent(prototype.getAction());
dummy.setType(prototype.getType());
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(dummy,0);
if (!resInfo.isEmpty())
{
for (ResolveInfo resolveInfo : resInfo)
{
if (resolveInfo.activityInfo == null
|| Arrays.asList(forbiddenChoices).contains(
resolveInfo.activityInfo.packageName))
continue;
//Get all the posible sharers
HashMap<String, String> info = new HashMap<String, String>();
info.put("packageName", resolveInfo.activityInfo.packageName);
info.put("className", resolveInfo.activityInfo.name);
String appName = String.valueOf(resolveInfo.activityInfo
.loadLabel(getPackageManager()));
info.put("simpleName", appName);
//Add only what we want
if (Arrays.asList(nameOfAppsToShareWith).contains(
appName.toLowerCase()))
{
intentMetaInfo.add(info);
}
}
if (!intentMetaInfo.isEmpty())
{
// sorting for nice readability
Collections.sort(intentMetaInfo,
new Comparator<HashMap<String, String>>()
{
@Override public int compare(
HashMap<String, String> map,
HashMap<String, String> map2)
{
return map.get("simpleName").compareTo(
map2.get("simpleName"));
}
});
// create the custom intent list
for (HashMap<String, String> metaInfo : intentMetaInfo)
{
Intent targetedShareIntent = (Intent) prototype.clone();
targetedShareIntent.setPackage(metaInfo.get("packageName"));
targetedShareIntent.setClassName(
metaInfo.get("packageName"),
metaInfo.get("className"));
targetedShareIntents.add(targetedShareIntent);
}
String shareVia = getString(R.string.offer_share_via);
String shareTitle = shareVia.substring(0, 1).toUpperCase()
+ shareVia.substring(1);
chooserIntent = Intent.createChooser(targetedShareIntents
.remove(targetedShareIntents.size() - 1), shareTitle);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[] {}));
return chooserIntent;
}
}
return Intent.createChooser(prototype,
getString(R.string.offer_share_via));
}
String[]nameOfAppsToShareWith=新字符串[]{“facebook”、“twitter”、“gmail”};
字符串[]黑名单=新字符串[]{“com.any.package”,“net.other.package”};
//你的共同意图
意向意向=新意向(意向.行动\发送);
intent.setType(“文本/普通”);
intent.putExtra(intent.EXTRA_TEXT,“某些文本”);
intent.putExtra(android.content.intent.EXTRA_SUBJECT,“一个SUBJECT”);
// ... 要添加的任何其他内容都可以调用自定义选择器
startActivity(generateCustomChooserIntent(意图,黑名单));
私人意图生成Stomchooserint(意图原型,
字符串[]禁止选择)
{
List targetedShareContents=new ArrayList();
List intentMetaInfo=new ArrayList();
意向选择内容;
Intent dummy=newintent(prototype.getAction());
setType(prototype.getType());
List resInfo=getPackageManager().QueryInputActivities(虚拟,0);
如果(!resInfo.isEmpty())
{
对于(ResolveInfo ResolveInfo:resInfo)
{
如果(resolveInfo.activityInfo==null
||asList(禁止选择)。包含(
resolveInfo.activityInfo.packageName)
继续;
//得到所有可能的分享者
HashMap info=新的HashMap();
info.put(“packageName”,resolveInfo.activityInfo.packageName);
info.put(“className”,resolveInfo.activityInfo.name);
String appName=String.valueOf(resolveInfo.activityInfo
.loadLabel(getPackageManager());
info.put(“simpleName”,appName);
//只添加我们想要的
if(Arrays.asList(nameOfAppsToShareWith).contains(
appName.toLowerCase())
{
intentMetaInfo.add(info);
}
}
如果(!intentMetaInfo.isEmpty())
{
//排序以获得良好的可读性
Collections.sort(intentMetaInfo,
新比较器()
{
@覆盖公共整数比较(
HashMap映射,
HashMap(地图2)
{
返回map.get(“simpleName”).compareTo(
map2.get(“simpleName”);
}
});
//创建自定义意图列表
对于(HashMap metaInfo:intentMetaInfo)
{
Intent targetedShareContent=(Intent)prototype.clone();
targetedShareContent.setPackage(metaInfo.get(“packageName”);
targetedShareIntent.setClassName(
metaInfo.get(“packageName”),
metaInfo.get(“className”);
targetedShareIntents.add(targetedShareIntent);
}
String shareVia=getString(R.String.offer\u share\u via);
字符串shareTitle=shareVia.substring(0,1).toUpperCase()
+shareVia.子串(1);
chooserentent=Intent.createChooser(targetedShareContents
.remove(targetedShareContents.size()-1),shareTitle);
选择content.putExtra(Intent.EXTRA\u初始意图,
目标共享内容toArray(新包裹[]{});
返回选择器内容;
}
}
返回Intent.createChooser(原型,
getString(R.string.offer_share_via));
}
我认为您正在寻找从其他应用程序接收简单数据的方法。如果是,则应为不同类型的数据使用android:mimeType条件。
在清单活动中放入以下代码
<activity android:name=".ui.MyActivity" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
}
我用add解决了这个问题 android:exported=“false”
下面是清单上的活动名称声明这是“”的副本吗?这回答了你的问题吗?@Beppe检查我的答案answer@HarshSharma谢谢你的回复。你的回答很好,但我想向用户显示可能的应用程序的屏幕。再次感谢你。实际上我的问题是关于什么时候我想分享一些东西。我希望“通过共享”屏幕显示,但我的应用程序不显示在该屏幕中@Ramachandra Singh谢谢。如果你想在文本库应用程序共享中显示你的应用程序,那么你必须在manifest@Ramachandra Singh中使用我使用的代码,你可以在我的问题中看到。但是你在活动中使用的源代码不正确。请检查参考网址已经有详细描述。我在我的应用程序中使用了相同的代码从其他应用程序中获取文本。@k3b请给我举个例子,因为我无法访问我所在区域的文档或链接。
void onCreate (Bundle savedInstanceState) {
...
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
} else if (type.startsWith("image/")) {
handleSendImage(intent); // Handle single image being sent
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
if (type.startsWith("image/")) {
handleSendMultipleImages(intent); // Handle multiple images being sent
}
} else {
// Handle other intents, such as being started from the home screen
}
...