2

I'm working on a Text base app that can share some parts to other apps in share via and also accept shared text from another apps. when I want to share my data, my app appeared in share via screen, how can remove my app from this screen,though I want when other app share some text , in "share via" screen , my app should place there. Here is my codes :

            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"));

and also:

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

thanks.

Beppe
  • 201
  • 1
  • 2
  • 13
  • is this a duplicate of "http://stackoverflow.com/questions/6408086/android-can-i-enable-disable-an-activitys-intent-filter-programmatically"? does this answer your question? – k3b Jan 27 '16 at 12:29
  • @Beppe Check my answer – Harsh Sharma Jan 27 '16 at 13:00
  • @HarshSharma thanks for reply. your answer is good but I want to show the screen of possible app to user . thank you again. – Beppe Jan 27 '16 at 13:40

3 Answers3

1

Found A way create a CustomChooser which block certain apps from displaying

Just pass your package name in String array blacklist and your app will be discarded.

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));
    }
Harsh Sharma
  • 898
  • 1
  • 14
  • 30
0

I think you are looking for Receiving Simple Data from Other Apps. If yes then should use android:mimeType condition for different type of data. Put below code in you manifest Activity.

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

Handle the Incoming Content

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
}
...

}

Source : http://developer.android.com/training/sharing/receive.html

Ramchandra Singh
  • 530
  • 4
  • 15
  • Actually my question is about when I want to share something. I want the "**share via**" screen show but my application don't show in that screen. @Ramachandra Singh thanks. – Beppe Jan 27 '16 at 13:47
  • if you want to show you app in text base app share then you have to use this code in manifest – Ramchandra Singh Jan 27 '16 at 13:52
  • @Ramachandra Singh I used it that you can see in my question. – Beppe Jan 27 '16 at 13:57
  • But you are not using correct source code in your activity. Please check reference url there already describe in detailed. I used same code in my apps to get text from other application. – Ramchandra Singh Jan 27 '16 at 14:01
0

I was solved this with add

android:exported="false"

below activity name declaration on manifest