According to the react native docs, excluding activity types for the share module is only available on IOS - https://facebook.github.io/react-native/docs/share.html. I'm trying to exclude email/sms for analytics purposes. Is there a way to circumvent this limitation or is this just not possible in Android?
Asked
Active
Viewed 1,379 times
1 Answers
1
This can be done using a react native module. Keep in mind that using this solution requires you having a keyword of the app's package name that you want to exclude.
Here is my native module:
package com.testproject;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Parcelable;
import android.util.Log;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import java.util.ArrayList;
import java.util.List;
public class ShareModule extends ReactContextBaseJavaModule {
public ShareModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "ShareExample";
}
private static boolean containsOneWord(String word, ReadableArray keywords) {
for (int i = 0; i < keywords.size(); i++)
if (word.contains(keywords.getString(i))) return true;
return false;
}
@ReactMethod
public void share(String subject, String message, ReadableArray toExclude) {
List<Intent> shareIntentsLists = new ArrayList<Intent>();
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
shareIntent.putExtra(Intent.EXTRA_TEXT, message);
List<ResolveInfo> resInfos = getCurrentActivity().getPackageManager().queryIntentActivities(shareIntent, 0);
if (!resInfos.isEmpty()) {
for (ResolveInfo resInfo : resInfos) {
String packageName = resInfo.activityInfo.packageName;
if (!containsOneWord(packageName.toLowerCase(), toExclude)) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, resInfo.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.setPackage(packageName);
shareIntentsLists.add(intent);
}
}
if (!shareIntentsLists.isEmpty()) {
Intent chooserIntent = Intent.createChooser(shareIntentsLists.remove(0), "Choose app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, shareIntentsLists.toArray(new Parcelable[]{}));
getCurrentActivity().startActivity(chooserIntent);
} else
Log.e("Error", "No Apps can perform your task");
}
}
}
This is how you would use it:
import { NativeModules } from 'react-native';
NativeModules.ShareExample.share('Hi', 'Hello world', ['mms', 'sms', 'messa', 'gm', 'mail', 'text']);
Keep in mind that different sms and email apps may have no keywords in common so include as much as you can.
For instructions on how to add a native module check the react native documentation here.
References:

user690944
- 26
- 1
- 2