android - 如何从 Android 共享中排除应用程序本身 (ShareActionProvider)

标签 android shareactionprovider

我在一个名为 GovDic 的应用程序中创建了一个共享功能。但是在共享列表中,出现了应用程序本身。如何将其从共享中排除?

我需要分享text/plain内容给其他App,其他App也可以分享text/plain内容给这个App。

list :

<?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>

Activity :

@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;
}

enter image description here

最佳答案

您好@user2299040 和@Marcin Orlowski:

谢谢,我修改了一些代码并且成功了:

菜单:

<?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:icon="@drawable/ic_share"
        android:title="@string/share" />
</menu>

Activity :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate menu resource file.
    getMenuInflater().inflate(R.menu.detail_menu, menu);
    // Return true to display menu
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.menu_item_share:
            Common.shareExcludingApp(this, "com.comp548.govdic", etDescription.getText().toString());
            break;
    }
    return super.onOptionsItemSelected(item);
}

分享功能:

public static void shareExcludingApp(Context ctx, String packageNameToExclude, String text) {

    List<Intent> targetedShareIntents = new ArrayList<>();
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType(Constant.MIME_TYPE_TEXT_PLAIN);
    List<ResolveInfo> resInfo = ctx.getPackageManager().queryIntentActivities(createShareIntent(text), 0);
    if (!resInfo.isEmpty()) {
        for (ResolveInfo info : resInfo) {
            Intent targetedShare = createShareIntent(text);

            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[targetedShareIntents.size()]));
        ctx.startActivity(chooserIntent);
    }

}

private static Intent createShareIntent(String text) {
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType(Constant.MIME_TYPE_TEXT_PLAIN);
    if (text != null) {
        share.putExtra(Intent.EXTRA_TEXT, text);
    }
    return share;
}

关于android - 如何从 Android 共享中排除应用程序本身 (ShareActionProvider),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39527053/

相关文章:

java - 在不同的任务中启动一个新的 Android Activity

android - 更改动态添加的 View 的高度

android - 在 Android Studio 中用 C++ 编写应用程序?

android - 使用 Picasso 的 ShareActionProvider 分享图像

android - 设置 ShareActionProvider 的颜色

android - 状态栏和导航栏上的 Google Now 渐变/阴影

java - 需要一个字符串,但 begin_array 位于第 1 行第 13 列

android - Android 2.3 的 ActionBarSherlock ShareActionProvider 不工作

android - 操作栏中的 ShareActionProvider 图标

android - 工具栏 ShareActionProvider 主题始终为深色