将自定义按钮添加到共享意图选择器

时间:2018-01-10 15:08:10

标签: android

是否可以将自定义按钮添加到共享意图选择器。 示例

enter image description here

我在网上搜索过,一无所获。

3 个答案:

答案 0 :(得分:0)

您必须在活动中添加意图过滤器才能完成任务。

<intent-filter
android:label="Share with my app">
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />

同时添加mime类型,意味着您将在intent filter中收到的内容:

<data android:mimeType="image/*" />
<data android:mimeType="text/*" />

但是,当其他应用向您的应用发送数据时,您必须在活动中接收数据 This tutorial will help

答案 1 :(得分:0)

您可以将活动添加到此列表中。您应该使用具有mime类型的特殊意图过滤器:

<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>
</activity>

有关详细信息,请参阅此处: https://developer.android.com/training/sharing/receive.html

答案 2 :(得分:0)

除了您制作包含共享应用程序和自定义按钮的自定义视图外,您不能这样做。虽然你可以,这是Android中的反模式。请参阅Google+ here上的Roman Nurik的帖子,第4项。

4)自定义非Android分享(不使用ACTION_SEND)

以下是如何执行此操作的示例代码

private void showCustomView() {
    PackageManager pm = activity.getPackageManager();
    List<ResolveInfo> resolveInfoList = pm.queryIntentActivities(getShareImageIntent("YOUR_IMAGE_URI"), 0);

    // code for showing your custom view
    // ...
    // do whatever you want with your custom buttons
    // end code for showing your custom view

    // BELOW IS HOW YOU COULD LAUNCH THE SPECIFIC APPLICATION WHEN ONE OF THE APPLICATION ITEMS CLICKED
    // from ResolveInfo you can get ActivityInfo
    ActivityInfo activityInfo = resolveInfo.activityInfo;
    // then you can create ComponentName
    ComponentName componentName = new ComponentName(activityInfo.applicationInfo.packageName, activityInfo.name);
    // launch the application you want to share
    Intent intent = getShareImageIntent("YOUR_IMAGE_URI");
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    intent.setComponent(componentName);
    context.startActivity(intent);
}

/**
 * Example method to get share intent for applications that receive image Uri
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@SuppressWarnings("deprecation")
private Intent getShareImageIntent(Uri imageUri) {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    else
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);

    shareIntent.setType("image/*");
    shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
    return shareIntent;
}