choosing certain apps to appear in the share chooser - android

I have a button that shares the link to my app;
but this button shows skype, gmail, whatsapp...
how can I only show whatsapp for example? what can I do?
Plus when sending a mail I need to choose only gmail and hotmail for example how to do that?
I mean I know how to choose an app such as whatsapp:
Sending message through WhatsApp
but how can I choose several apps..
thanks.
Edit
Does someone have a tutorial on how to create your own share chooser? thanks.

Create your own chooser and use the following codes:
public void share_on_gmail()
{
Intent sharingIntent = is_intent_available("com.google.android.gm",
"com.google.android.gm.ComposeActivityGmail", "Gmail");
send_mail(sharingIntent);
}
public void share_on_yahoo()
{
Intent sharingIntent = is_intent_available("com.android.email",
"com.android.email.activity.MessageCompose", "Email");
send_mail(sharingIntent);
}
public void share_on_facebook()
{
Intent sharingIntent = is_intent_available("com.facebook.katana",
"com.facebook.katana.ShareLinkActivity", "Facebook");
}
public void share_on_twitter()
{
Intent sharingIntent = is_intent_available("com.twitter.android",
"com.twitter.android.PostActivity", "Twitter");
if (sharingIntent != null)
{
String subject = ((XmlNewsDetailsParser)xmlParser).subject;
String body = ((XmlNewsDetailsParser)xmlParser).body;
File mFile = savebitmap(((Picture)itemsAdapter.getItem(0)).image.bitmap);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mFile));
sharingIntent.putExtra(Intent.EXTRA_TEXT, "القارئ العربي" + "\n\n" + subject + "\n\n" + Html.fromHtml(body));
startActivity(sharingIntent);
}
}
public Intent is_intent_available(final String className, String activityName, String appName)
{
Intent I = new Intent(android.content.Intent.ACTION_SEND);
if (!activityName.equals(""))
{
I.setClassName(className, activityName);
}
else
{
I.setPackage(className);
}
PackageManager packageManager = getPackageManager();
List list = packageManager.queryIntentActivities(I, PackageManager.MATCH_DEFAULT_ONLY);
if (list==null || list.size() == 0)
{
AlertDialog ad = new AlertDialog.Builder(this).create();
ad.setMessage("Please install the " + appName + " app to share news with your friends.");
ad.setButton2("Later", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.setButton("Install Now", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=" + className));
startActivity(marketIntent);
}
});
ad.show();
return null;
}
return I;
}
public void send_mail(Intent sharingIntent)
{
if (sharingIntent == null) return;
String subject = ((XmlNewsDetailsParser)xmlParser).subject;
String body = ((XmlNewsDetailsParser)xmlParser).body;
File mFile = savebitmap(((Picture)itemsAdapter.getItem(0)).image.bitmap);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, subject + "القارئ العربي - ");
sharingIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body));
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mFile));
startActivity(sharingIntent);
}
I have the class name and the activity name for a couple of more applications. I can provide it. Try those and I will modify the answer.

Related

Share image through share intent to Whatsapp

here is the code through which i am sharing my image or video from an adaptor to WhatsApp it was working fine but now just the toast is shown that whats app not install is there any issue in the code am I missing something?
public void shareWhatsapp(String type, String path, String package_name) {
Uri uri = FileProvider.getUriForFile(mFragment, BuildConfig.APPLICATION_ID + ".provider", new File(path));
PackageManager pm = mFragment.getPackageManager();
try {
PackageInfo info = pm.getPackageInfo(package_name, PackageManager.GET_META_DATA);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sharingIntent.setType(type);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
sharingIntent.setPackage(package_name);
mFragment.startActivity(Intent.createChooser(sharingIntent, "Share via"));
} catch (PackageManager.NameNotFoundException e) {
Toast.makeText(mFragment, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}
here is the listener
holder.repostWhatsapp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final ModelStatus curVideo = getItem(position);
if (curVideo.getFull_path().endsWith(".jpg")) {
shareWhatsapp("image/jpg", curVideo.getFull_path(), "com.whatsapp");
} else if (curVideo.getFull_path().endsWith(".mp4")) {
shareWhatsapp("video/mp4", curVideo.getFull_path(), "com.whatsapp");
}
}
});
use below code to share image or video to whatsapp
holder.repostWhatsapp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = FileProvider.getUriForFile(PdfRendererActivity.this,
PdfRendererActivity.this.getPackageName() + ".provider", outputFile);
if (curVideo.getFull_path().endsWith(".jpg")) {
shareWhatsapp("image/jpg", uri);
} else if (curVideo.getFull_path().endsWith(".mp4")) {
shareWhatsapp("video/mp4", uri);
}
}
});
shareWhatsApp
void shareWhatsapp(String type, String uri)
{
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType(type);
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setPackage("com.whatsapp");
startActivity(share);
}

Anchor tag not working using Intent Share-Android

I want to share text, Image with clickable links in Gmail, facebook, twitter etc using intents.below is my code but it does not work. I have also put in link text in String resource file but does not work.The sharing functions as it should however when shared in a Gmail, Facebook, Twitter, etc. the link is ignored and the Gmail or tweet shows only plain text like this
private void share(Uri bmpUri)
{
String hyperlink_url="https://www.google.co.in/?gfe_rd=cr&ei=_3edWb68K4rT8gfx_pCoDw";
String text = "here";
String body = "Click";
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hey! I just saw ABC is having XYZ through the my App! We should check it out."+ "\n" + Html.fromHtml(body));
shareIntent.setType("text/html");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "");
//String[] mimetypes = {"image/*", "video/*","text/html"};
// shareIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
final List<ResolveInfo> activities = getPackageManager().queryIntentActivities(shareIntent, 0);
List<String> appNames = new ArrayList<String>();
for (ResolveInfo info : activities) {
appNames.add(info.loadLabel(getPackageManager()).toString());
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Complete Action using...");
builder.setItems(appNames.toArray(new CharSequence[appNames.size()]), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
ResolveInfo info = activities.get(item);
if (info.activityInfo.packageName.equals("com.facebook.katana")) {
// Facebook was chosen
// shareToFacebook();
} else if (info.activityInfo.packageName.equals("com.twitter.android")) {
// Twitter was chosen
shareIntent.setPackage(info.activityInfo.packageName);
startActivity(shareIntent);
} else {
// start the selected activity
shareIntent.setPackage(info.activityInfo.packageName);
startActivity(shareIntent);
}
}
});
AlertDialog alert = builder.create();
alert.show();
}

Android - share (java code) number

How I share (java code) from my android application a phone number, so that it can mark it or send it by email at a time.
Thank you very much
Here's my code:
#Override
public void onClick(View view) {
TextView txtphone = (TextView) findViewById(R.id.txtphone1);
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + txtphone.getText()));
try {
startActivity(intent);
}catch (Throwable e) {
message("ERROR: startActivity - " + e.toString());
return;
}
return;
}
try this
public static void shareText(#NonNull Activity activity, #NonNull String text) {
Intent shareIntent = ShareCompat.IntentBuilder.from(activity)
.setType("text/plain")
.setText(text)
.getIntent();
if (shareIntent.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivity(shareIntent);
}
}
here's a great article about sharing - https://medium.com/google-developers/sharing-content-between-android-apps-2e6db9d1368b#.ommhqdbhy

How can I share text in what's app on particular Number

Using this code only open particulat number's chat but Text is not share.How can I do this?
public class MainActivity extends AppCompatActivity {
Button Wa;
String id = "+919000000000";
EditText txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (EditText)findViewById(R.id.editText);
Wa = (Button)findViewById(R.id.btn_whatsapp);
Wa.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("smsto:" + id);
Intent waIntent = new Intent(Intent.ACTION_SENDTO,uri);
String text = "testing message";
waIntent.setPackage("com.whatsapp");
if (waIntent != null) {
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, text));
} else {
Toast.makeText(getApplicationContext(), "WhatsApp not found", Toast.LENGTH_SHORT)
.show();
}
}
});
}
Since you trying to achieve it as "smsto:", "text/plain" as type will help you. Try Extra as "sms_body" if it won't help.
Uri uri = Uri.parse("smsto:" + id);
Intent waIntent = new Intent(Intent.ACTION_SENDTO,uri);
String text = "testing message";
waIntent.setPackage("com.whatsapp");
if (waIntent != null) {
waIntent.setType("text/plain");
//waIntent.putExtra(Intent.EXTRA_TEXT, text);
waIntent.putExtra("sms_body", text);
startActivity(Intent.createChooser(waIntent, text));
} else {
Toast.makeText(getApplicationContext(), "WhatsApp not found", Toast.LENGTH_SHORT)
.show();
}
Please go through this stackoverflow links
Sending message through WhatsApp
Send text to specific contact
It seems that WhatsApp still does't have supported this fetaure.
You can only open chat history for particular number using below code
try {
Uri mUri = Uri.parse("smsto:+98xxxxxxxx");
Intent mIntent = new Intent(Intent.ACTION_SENDTO, mUri);
mIntent.setPackage("com.whatsapp");
// mIntent.putExtra("sms_body", "My body");
mIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
startActivity(mIntent);
} catch (Exception e) {
// alert WhatsApp in not installed
}
Answer from Here
Earlier it wasn't possible but since the May '15 update. Checkout :
try{
PackageInfo info = pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
String sendString = "some random string";
sendIntent.setPackage("com.whatsapp");
sendIntent.putExtra(Intent.EXTRA_TEXT, sendString);
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
sendIntent.setType("image/*");
startActivity(sendIntent);
} catch (Exception e){
// some code
}
Here PackageInfo line is just to check if WhatsApp is installed. It throws Exception if not. You can just ignore that if you want to do a normal share (and setPackage also).
Also. It is important that the media you want to share has to be publicly available on local storage.
UPDATE
To send to a specific contact
Uri uri = Uri.parse("smsto:" + "<CONTACT_NUMBER>");
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);
As Action Send To is now allowed.
Try something like this:
public class MainActivity extends AppCompatActivity {
Button Wa;
String id = "+919000000000";
EditText txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (EditText)findViewById(R.id.editText);
Wa = (Button)findViewById(R.id.btn_whatsapp);
Wa.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PackageManager pm=getPackageManager();
try {
Uri uri = Uri.parse("smsto:" + number);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
waIntent.setType("text/plain");
String text = "testing message";
PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, text));
}
catch (NameNotFoundException e) {
Toast.makeText(this, "WhatsApp not found", Toast.LENGTH_SHORT).show();
}
}
});
}
}

How can I post on Twitter with Intent Action_send?

I have been struggling to send text from my app to Twitter.
The code below works to bring up a list of apps such as Bluetooth, Gmail, Facebook and Twitter, but when I select Twitter it doesn't prefill the text as I would have expected.
I know that there are issues around doing this with Facebook, but I must be doing something wrong for it to not be working with Twitter.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Example Text");
startActivity(Intent.createChooser(intent, "Share Text"));
I'm using this snippet on my code:
private void shareTwitter(String message) {
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "This is a Test.");
tweetIntent.setType("text/plain");
PackageManager packManager = getPackageManager();
List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
boolean resolved = false;
for (ResolveInfo resolveInfo : resolvedInfoList) {
if (resolveInfo.activityInfo.packageName.startsWith("com.twitter.android")) {
tweetIntent.setClassName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name);
resolved = true;
break;
}
}
if (resolved) {
startActivity(tweetIntent);
} else {
Intent i = new Intent();
i.putExtra(Intent.EXTRA_TEXT, message);
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://twitter.com/intent/tweet?text=" + urlEncode(message)));
startActivity(i);
Toast.makeText(this, "Twitter app isn't found", Toast.LENGTH_LONG).show();
}
}
private String urlEncode(String s) {
try {
return URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.wtf(TAG, "UTF-8 should always be supported", e);
return "";
}
}
Hope it helps.
you can simply open the URL with the text and Twitter App will do it. ;)
String url = "http://www.twitter.com/intent/tweet?url=YOURURL&text=YOURTEXT";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
and it will also open the browser to login at the tweet if twitter app is not found.
Try this, I used it and worked great
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/intent/tweet?text=...."));
startActivity(browserIntent);
First you have to check if the twitter app installed on the device or not then share the text on twitter:
try
{
// Check if the Twitter app is installed on the phone.
getActivity().getPackageManager().getPackageInfo("com.twitter.android", 0);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setClassName("com.twitter.android", "com.twitter.android.composer.ComposerActivity");
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Your text");
startActivity(intent);
}
catch (Exception e)
{
Toast.makeText(getActivity(),"Twitter is not installed on this device",Toast.LENGTH_LONG).show();
}
For sharing text and image on Twitter, more controlled version of code is below, you can add more methods for sharing with WhatsApp, Facebook ... This is for official App and does not open browser if app not exists.
public class IntentShareHelper {
public static void shareOnTwitter(AppCompatActivity appCompatActivity, String textBody, Uri fileUri) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.setPackage("com.twitter.android");
intent.putExtra(Intent.EXTRA_TEXT,!TextUtils.isEmpty(textBody) ? textBody : "");
if (fileUri != null) {
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/*");
}
try {
appCompatActivity.startActivity(intent);
} catch (android.content.ActivityNotFoundException ex) {
ex.printStackTrace();
showWarningDialog(appCompatActivity, appCompatActivity.getString(R.string.error_activity_not_found));
}
}
public static void shareOnWhatsapp(AppCompatActivity appCompatActivity, String textBody, Uri fileUri){...}
private static void showWarningDialog(Context context, String message) {
new AlertDialog.Builder(context)
.setMessage(message)
.setNegativeButton(context.getString(R.string.close), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setCancelable(true)
.create().show();
}
}
For getting Uri from File, use below class:
public class UtilityFile {
public static #Nullable Uri getUriFromFile(Context context, #Nullable File file) {
if (file == null)
return null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
return FileProvider.getUriForFile(context, "com.my.package.fileprovider", file);
} catch (Exception e) {
e.printStackTrace();
return null;
}
} else {
return Uri.fromFile(file);
}
}
// Returns the URI path to the Bitmap displayed in specified ImageView
public static Uri getLocalBitmapUri(Context context, ImageView imageView) {
Drawable drawable = imageView.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable) {
bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
return null;
}
// Store image to default external storage directory
Uri bmpUri = null;
try {
// Use methods on Context to access package-specific directories on external storage.
// This way, you don't need to request external read/write permission.
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = getUriFromFile(context, file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
}
For writing FileProvider, use this link: https://github.com/codepath/android_guides/wiki/Sharing-Content-with-Intents

Categories

Resources