如何从Android共享(ShareActionProvider)中排除应用程序本身 [英] How to exclude the application itself from Android share (ShareActionProvider)

查看:226
本文介绍了如何从Android共享(ShareActionProvider)中排除应用程序本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为GovDic的应用程序中实现了共享功能.但是在共享列表中,应用程序本身出现了.如何从共享中排除它?

I made a share function in a App called GovDic. But in the share list, the application itself appeared. How to exclude it from the share?

我需要与其他应用共享text/plain内容,其他应用也可以与该应用共享text/plain内容.

I need share text/plain content to other App, also other App can share text/plain content to this App.

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.comp548.govdic">

    ...

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        ...

        <activity android:name=".OrgDetailActivity">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>

            <meta-data android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity"/>
        </activity>
    </application>

</manifest>

菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/menu_item_share"
        app:showAsAction="ifRoom"
        android:title="@string/share"
        app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>

活动:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate menu resource file.
    getMenuInflater().inflate(R.menu.detail_menu, menu);

    // Locate MenuItem with ShareActionProvider
    MenuItem menuItem = menu.findItem(R.id.menu_item_share);

    // Fetch and store ShareActionProvider
    ShareActionProvider shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);

    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType(Constant.MIME_TYPE_TEXT_PLAIN);
    shareIntent.putExtra(Intent.EXTRA_TEXT, etDescription.getText().toString());

    shareActionProvider.setShareIntent(shareIntent);

    // Return true to display menu
    return true;
}

推荐答案

这些功能可以帮助您实现所需的目标.只需输入您的包裹名称

these functions can help you achieve you want. just pass your package name

public static void shareExludingApp(Context ctx, String packageNameToExclude, android.net.Uri imagePath, String text) {

        List<Intent> targetedShareIntents = new ArrayList<Intent>();
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("image/*");
        File file = new File(imagePath.getPath());
        List<ResolveInfo> resInfo = ctx.getPackageManager().queryIntentActivities(createShareIntent(text, file), 0);
        if (!resInfo.isEmpty()) {
            for (ResolveInfo info : resInfo) {
                Intent targetedShare = createShareIntent(text, file);

                if (!info.activityInfo.packageName.equalsIgnoreCase(packageNameToExclude)) {
                    targetedShare.setPackage(info.activityInfo.packageName);
                    targetedShareIntents.add(targetedShare);
                }
            }

            Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0),
                    "Select app to share");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                    targetedShareIntents.toArray(new Parcelable[]{}));
            ctx.startActivity(chooserIntent);
        }

    }

    private static Intent createShareIntent(String text, File file) {
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("image/*");
        if (text != null) {
            share.putExtra(Intent.EXTRA_TEXT, text);
        }
        share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        return share;
    }

这篇关于如何从Android共享(ShareActionProvider)中排除应用程序本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆