How come Twitter, Facebook, and Google+ does not display on chooserIntent? - android

I am trying to add Twitter, Facebook and Google Plus to my chooserIntent, and all I am getting are email, gmail, and messaging options. I have different messages depending upon which social media is selected. I believe I am missing something to force these apps on my list. My test device has all three of these apps installed too.
I call shareIntent() method for this.
private Intent shareIntent() {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
shareIntent.setType("text/plain");
String shareMsg = "Message 1";
String emailMsg = "Message 2";
PackageManager pm = getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
String packageName = app.activityInfo.packageName;
Log.d(TAG, "packageName(" + packageName +")");
Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
targetedShareIntent.setType("text/plain");
targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"SUBJECT");
if (TextUtils.equals(packageName, "com.twitter.android")) {
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
shareMsg);
} else if (TextUtils.equals(packageName, "com.facebook.android")) {
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
shareMsg);
} else if (TextUtils.equals(packageName, "com.google.android.apps.plus")) {
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
shareMsg);
} else if (TextUtils.equals(packageName, "com.google.android.gm")) {
targetedShareIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] {"email#hotmail.com", "email#gmail.com"});
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
emailMsg);
} else if (TextUtils.equals(packageName, "com.android.email")) {
targetedShareIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] {"email#hotmail.com", "email#gmail.com"});
} else if (TextUtils.equals(packageName, "com.android.ms")) {
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
shareMsg);
}
targetedShareIntent.setPackage(packageName);
targetedShareIntents.add(targetedShareIntent);
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0),
"Share via");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
return shareIntent;
}

Try out with the below PackageNames
For Twitter:
if ("com.twitter.android.PostActivity".equals(app.activityInfo.name))
For Facebook:
if ((app.activityInfo.name).contain("facebook"))
For Gmail:
if ((app.activityInfo.name).contain("gmail"))
Check out HERE

I have a solution. Take a look. Thanks to everyone that helped.
private Intent shareIntent() {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
shareIntent.setType("text/plain");
String shareMsg = "Message 1";
String emailMsg = "Message 2";
PackageManager pm = getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
String packageName = app.activityInfo.packageName;
Log.d(TAG, "packageName(" + packageName +")");
Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
targetedShareIntent.setType("text/plain");
targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"Subject");
if (TextUtils.equals(packageName, "com.google.android.gm")) {
Log.d(TAG, "gmail: " + app.activityInfo.name);
targetedShareIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] {"email#hotmail.com", "email#gmail.com"});
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
emailMsg);
} else if (TextUtils.equals(packageName, "com.android.email")) {
Log.d(TAG, "email: " + app.activityInfo.name);
targetedShareIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] {"email#hotmail.com", "email#gmail.com"});
} else if (TextUtils.equals(packageName, "com.android.mms")) {
Log.d(TAG, "text msg: " + app.activityInfo.name);
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
shareMsg);
}
targetedShareIntent.setPackage(packageName);
targetedShareIntents.add(targetedShareIntent);
}
Intent facebookIntent = getShareIntent("facebook", "Tip Calculator", shareMsg);
if(facebookIntent != null)
targetedShareIntents.add(facebookIntent);
Intent twitterIntent = getShareIntent("twitter", "Tip Calculator", shareMsg);
if(twitterIntent != null)
targetedShareIntents.add(twitterIntent);
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0),
"Share via");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
return shareIntent;
}
And you need this other method as well.
private Intent getShareIntent(String type, String subject, String text)
{
boolean found = false;
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
// gets the list of intents that can be loaded.
List<ResolveInfo> resInfo = this.getPackageManager().queryIntentActivities(share, 0);
System.out.println("resinfo: " + resInfo);
if (!resInfo.isEmpty()){
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase().contains(type) ||
info.activityInfo.name.toLowerCase().contains(type) ) {
share.putExtra(Intent.EXTRA_SUBJECT, subject);
share.putExtra(Intent.EXTRA_TEXT, text);
share.setPackage(info.activityInfo.packageName);
found = true;
break;
}
}
if (!found)
return null;
return share;
}
return null;
}
Hopefully this will help someone else in the future.

Related

Default Messaging App version 4.2.1 is not able show in intent chooser dialog with other apps like whatsapp, etc

I want to show only Messaging app and Whatsapp application in Intentchooser. The following code is working fine with some other devices like moto e, g, etc.. But in some other devices its now showing the default messaging application. This is my code. Please help me.
private void shareTheApp() {
Resources resources = getResources();
Intent emailIntent = new Intent();
emailIntent.setAction(Intent.ACTION_SEND);
PackageManager pm = getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 0; i < resInfo.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
emailIntent.setAction(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
if (packageName.contains("com.whatsapp")) {
emailIntent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_text));
emailIntent.setType("text/plain");
emailIntent.setPackage(packageName);
}
if (packageName.contains("mms")) {
isMmsAvailable = true;
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
if (packageName.contains("mms")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_text));
}
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
}
if(!isMmsAvailable) {
isMmsAvailable = false;
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setData(Uri.parse("sms:"));
smsIntent.putExtra("sms_body", resources.getString(R.string.share_text));
intentList.add(new LabeledIntent(smsIntent, "", "Messaging", R.drawable.ic_launcher));
}
// convert intentList to array
LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
Intent openInChooser = Intent.createChooser(emailIntent, getResources().getString(R.string.openchooser_tittle));
openInChooser.addCategory(Intent.CATEGORY_DEFAULT);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);
}
Yes, I solved this problem :) below the code.
Resources resources = getResources();
List<Intent> selectedIntents = new ArrayList<Intent>();
Intent shareIntent=new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
List<ResolveInfo> resInfos=getPackageManager().queryIntentActivities(shareIntent, 0);
if(!resInfos.isEmpty()){
for(ResolveInfo resInfo : resInfos) {
String packageName=resInfo.activityInfo.packageName;
if(packageName.contains("com.whatsapp")){
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_TEXT, resources.getString(R.string.share_text));
intent.setPackage(packageName);
selectedIntents.add(intent);
}
}
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setType("vnd.android-dir/mms-sms");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(sendIntent, 0);
for (int i = 0; i < resInfo.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
if (packageName.contains("mms")) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
if (packageName.contains("mms")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_text));
selectedIntents.add(intent);
}
}
}
if(!selectedIntents.isEmpty()){
Intent chooserIntent = null;
chooserIntent = Intent.createChooser(selectedIntents.get(0), resources.getString(R.string.openchooser_tittle));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, selectedIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
}
we need to call both action ACTION_SEND and ACTION_VIEW. Now it will show in intent chooser for both type of messaging app. Hope it might be usefull for some else.

android filter app returns two twitter

I am trying to filter the app i have in the phone, facebook and twitter are what I want to find. Below is the code i use, and it works fine before, it filter twitter and facebook for me to post something. Then later it become i filter two twitter apps. Anyone know why?
I use debug more and found these, how come i get two twitter package. I have tried delete and reinstall my twitter app, it won't work.
ResolveInfo{445f8600 com.twitter.android/.composer.ComposerActivity m=0x608000}
ResolveInfo{445f14d0 com.twitter.android/.DMActivity m=0x608000}
private void shareContent() {
UtuBaseActivity activity = (UtuBaseActivity) getActivity();
if (activity == null || activity.isFinishing())
return;
if (promotionDetail == null)
return;
String title = getResources().getString(R.string.share_chooser_title);
String app_name = getResources().getString(R.string.app_name);
List<Intent> targetShareIntents = new ArrayList<>();
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
List<ResolveInfo> resInfos = activity.getPackageManager().queryIntentActivities(shareIntent, 0);
if (!resInfos.isEmpty()) {
for (ResolveInfo resInfo : resInfos) {
String packageName = resInfo.activityInfo.packageName;
if (packageName.contains("com.twitter.android") || packageName.contains("com.facebook.katana")) {
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, app_name);
intent.putExtra(Intent.EXTRA_TEXT, promotionDetail.getCoverpicture());
intent.setPackage(packageName);
targetShareIntents.add(intent);
}
}
if (!targetShareIntents.isEmpty()) {
Intent chooserIntent = Intent.createChooser(targetShareIntents.remove(0), title);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
} else {
// As fallback, launch sharer.php in a browser
String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + promotionDetail.getCoverpicture();
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
startActivity(i);
}
}
}
add this, it works tested.
check parent name equals to com.twitter.android.MainActivity
private void shareContent() {
UtuBaseActivity activity = (UtuBaseActivity) getActivity();
if (activity == null || activity.isFinishing())
return;
if (promotionDetail == null)
return;
String title = getResources().getString(R.string.share_chooser_title);
String app_name = getResources().getString(R.string.app_name);
List<Intent> targetShareIntents = new ArrayList<>();
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
List<ResolveInfo> resInfos = activity.getPackageManager().queryIntentActivities(shareIntent, 0);
if (!resInfos.isEmpty()) {
for (ResolveInfo resInfo : resInfos) {
String packageName = resInfo.activityInfo.packageName;
String parentName = resInfo.activityInfo.parentActivityName;
if (packageName.contains("com.twitter.android") || packageName.contains("com.facebook.katana")) {
if (packageName.contains("com.twitter.android"))
if (!parentName.equals("com.twitter.android.MainActivity"))
continue;
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, app_name);
intent.putExtra(Intent.EXTRA_TEXT, promotionDetail.getCoverpicture());
intent.setPackage(packageName);
targetShareIntents.add(intent);
}
}
if (!targetShareIntents.isEmpty()) {
Intent chooserIntent = Intent.createChooser(targetShareIntents.remove(0), title);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
} else {
// As fallback, launch sharer.php in a browser
String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + promotionDetail.getCoverpicture();
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
startActivity(i);
}
}
}

ShareActionProvider Android system menu

I have a little problem in my menu. I click in my MenuItem, then Share and I would like open my Intent (Chooser). But I've android system item between.
My code :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem item = menu.findItem(R.id.file);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
setShareIntent();
return true;
}
private void setShareIntent() {
Resources resources = getResources();
Intent emailIntent = new Intent();
emailIntent.setAction(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_native)));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
emailIntent.setType("message/rfc822");
PackageManager pm = getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
Intent openInChooser = Intent.createChooser(emailIntent, resources.getString(R.string.share_chooser_text));
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 0; i < resInfo.size(); i++) {
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
if(packageName.contains("android.email")) {
emailIntent.setPackage(packageName);
} else if(packageName.contains("twitter") || packageName.contains("facebook") || packageName.contains("mms") || packageName.contains("android.gm")) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
if(packageName.contains("twitter")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_twitter));
} else if(packageName.contains("facebook")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_facebook));
} else if(packageName.contains("mms")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_sms));
} else if(packageName.contains("android.gm")) {
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_gmail)));
intent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
intent.setType("message/rfc822");
}
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
}
LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
mShareActionProvider.setShareIntent(openInChooser);
//startActivity(openInChooser);
}
Is this line ? :
mShareActionProvider.setShareIntent(openInChooser);
I don't see.
My menu :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.app.FragmentsSliderNews" >
<item android:id="#+id/file"
android:title="Share"
android:icon="#drawable/ic_action_share"
app:actionProviderClass=
"android.support.v7.widget.ShareActionProvider">
</item>
</menu>
Thank you all for yours answers.
Get rid of createChooser(). You only use that with startActivity(), not for things like ShareActionProvider.
share via option in android
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "News for you!");
intent.putExtra(android.content.Intent.EXTRA_TEXT,"Additional text");
startActivity(Intent.createChooser(intent, "Share via"));
** its possible Check out below which shows the filteration for Facebook,Gmail,Twitter.**
Updated to Share Text + Image:
Select the image from the SDCard:
String fileName = "image-3116.jpg";
String externalStorageDirectory =
Environment.getExternalStorageDirectory().toString();
String myDir = externalStorageDirectory + "/saved_images/"; // the
// file will be in saved_images
Uri uri = Uri.parse("file:///" + myDir + fileName);
enter code here
Share via Twitter
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
(String)v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String)
v.getTag(R.drawable.ic_launcher));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ("com.twitter.android.PostActivity".equals(app.activityInfo.name))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new
ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Share via Facebook
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String)
v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String)
v.getTag(R.drawable.ic_launcher));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ((app.activityInfo.name).contains("facebook"))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new
ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Share via Gmail
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String)
v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String)
v.getTag(R.drawable.ic_launcher));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ((app.activityInfo.name).contains("android.gm"))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new
ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
It's ok, I changed my code for :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem item = menu.findItem(R.id.file);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
mShareActionProvider.setOnShareTargetSelectedListener(this);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
openShare();
return false;
}
});
return true;
}
private void openShare() {
Resources resources = getResources();
Intent emailIntent = new Intent();
emailIntent.setAction(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_native)));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
emailIntent.setType("message/rfc822");
PackageManager pm = getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
Intent openInChooser = Intent.createChooser(emailIntent, resources.getString(R.string.share_chooser_text));
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 0; i < resInfo.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
if(packageName.contains("android.email")) {
emailIntent.setPackage(packageName);
} else if(packageName.contains("twitter") || packageName.contains("facebook") || packageName.contains("mms") || packageName.contains("android.gm")) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
if(packageName.contains("twitter")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_twitter));
} else if(packageName.contains("facebook")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_facebook));
} else if(packageName.contains("mms")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_sms));
} else if(packageName.contains("android.gm")) {
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_gmail)));
intent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
intent.setType("message/rfc822");
}
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
}
LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);
}
But I need to custom BackgroundColor :-)

IntentChooser - different intents for different apps

I've found this answer, which explains how to create a different intent for each app, but some why it does not work:
here is the code that reproduces the issue, you can even comment the if and you will stick get the text and not the twitter
public Intent getShareChooserIntent(Context context) {
Intent basicIntent = new Intent();
basicIntent.setAction(Intent.ACTION_SEND);
basicIntent.setType("text/plain");
basicIntent.putExtra(Intent.EXTRA_TEXT, "text");
basicIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
PackageManager pm = context.getPackageManager();
Intent openInChooser = Intent.createChooser(basicIntent, "share");
List<ResolveInfo> resInfo = pm.queryIntentActivities(basicIntent, 0);
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 0; i < resInfo.size(); i++) {
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
Intent realIntent = new Intent();
realIntent.setAction(Intent.ACTION_SEND);
realIntent.setType("text/plain");
realIntent.putExtra(Intent.EXTRA_TEXT, "text");
realIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
if (packageName.contains("twitter")) // !!!!THIS LINE!!!!
realIntent.putExtra(Intent.EXTRA_TEXT, "twitter");
intentList.add(new LabeledIntent(realIntent, packageName, ri.loadLabel(pm), ri.icon));
}
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray( new LabeledIntent[ intentList.size()]));
return openInChooser;
}
what am I doing wrong?
Not sure what getShareBasicIntent do, but this code works fine for me (checked it with and without twitter)
static public Intent getShareChooserIntent(Context context) {
PackageManager pm = context.getPackageManager();
// Note that this intent should only be for email
// to avoid duplicates in the list
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_TEXT, "not twitter");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.setType("message/rfc822");
Intent openInChooser = Intent.createChooser(emailIntent, "share");
// the basic intent used to query all the activities that can support
// text sharing
Intent basicIntent = new Intent();
basicIntent.setAction(Intent.ACTION_SEND);
basicIntent.setType("text/plain");
List<ResolveInfo> resInfo = pm.queryIntentActivities(basicIntent, 0);
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 0; i < resInfo.size(); i++) {
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
Intent intent=new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "not twitter");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.setPackage(packageName);
if (packageName.contains("twitter")) {
intent.putExtra(Intent.EXTRA_TEXT, "twitter");
}
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray( new LabeledIntent[ intentList.size()]));
return openInChooser;
}

Customize Android Intent.ACTION_SEND

I'm using Intent for sharing url and subject. In this intent filter showing all the sharing apps. i want only (facebook/gmail/message/skype/twitter) these option in popup. is this possible to customize sharing intent filter.
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = adapter.getDetails("url";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"subject");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
thanks
Yes, its possible Check out below which shows the filteration for Facebook,Gmail,Twitter.
Updated to Share Text + Image:
Select the image from the SDCard:
String fileName = "image-3116.jpg";
String externalStorageDirectory = Environment.getExternalStorageDirectory().toString();
String myDir = externalStorageDirectory + "/saved_images/"; // the
// file will be in saved_images
Uri uri = Uri.parse("file:///" + myDir + fileName);
Share via Twitter
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.drawable.ic_launcher));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ("com.twitter.android.PostActivity".equals(app.activityInfo.name))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Share via Facebook
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.drawable.ic_launcher));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ((app.activityInfo.name).startsWith("com.facebook.katana"))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Share via Gmail
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.drawable.ic_launcher));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ((app.activityInfo.name).contains("android.gm"))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Share via WhatsApp:
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/html");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.drawable.ic_launcher));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
if ((app.activityInfo.name).contains("com.whatsapp")) {
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(
activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Single method for multiple functionalities
Code of share(String nameApp,String imagePath,String message) function:
public void share(String nameApp, String imagePath, String message) {
try {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
List<ResolveInfo> resInfo = getPackageManager()
.queryIntentActivities(share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(
android.content.Intent.ACTION_SEND);
targetedShare.setType("image/jpeg"); // put here your mime
// type
if (info.activityInfo.packageName.toLowerCase().contains(
nameApp)
|| info.activityInfo.name.toLowerCase().contains(
nameApp)) {
targetedShare.putExtra(Intent.EXTRA_SUBJECT,
"Sample Photo");
targetedShare.putExtra(Intent.EXTRA_TEXT, message);
targetedShare.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(new File(imagePath)));
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[] {}));
startActivity(chooserIntent);
}
} catch (Exception e) {
Log.v("VM",
"Exception while sending image on" + nameApp + " "
+ e.getMessage());
}
}
For attaching image on gmail, facebook, twitter with text use below code.
File filePath = new File("your image path");
share("gmail", filePath.toString(),"your comment");
share("facebook", filePath.toString(),"your comment");
share("twitter", filePath.toString(),"your comment");
Above given answer open facebook chat app on my phone. For the following worked !!
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Text...");
PackageManager packManager = getPackageManager();
List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(sharingIntent, PackageManager.MATCH_DEFAULT_ONLY);
boolean resolved = false;
for(ResolveInfo resolveInfo: resolvedInfoList){
if(resolveInfo.activityInfo.packageName.startsWith("com.facebook.katana")){
sharingIntent.setClassName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name );
resolved = true;
break;
}
}
if(resolved){
startActivity(sharingIntent);
}else{
Builder alert = new AlertDialog.Builder(ActivityName.this);
alert.setTitle("Warning");
alert.setMessage("Facebook App not found");
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which)
{
dialog.dismiss();
}
});
alert.show();
}
You can't share Extras with image without using facebook sdk.
https://developers.facebook.com/x/bugs/332619626816423/

Categories

Resources