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
Related
i have a ImageViewbutton which opens gmail compose mail tab in my android app,via intent.
Previously the same code was working for opening it.
imageView3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent send=new Intent(Intent.ACTION_VIEW);
send.setType("plain/text");
send.setData(Uri.parse("testmail#gmail.com"));
send.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
startActivity(send);
}
});
Now the app gets crashed after clicking on the button.
replace your code with this. if your app not crashing anymore it means you have not Gmail app in your android device.
imageView3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
try
{
Intent send=new Intent(Intent.ACTION_VIEW);
send.setType("plain/text");
send.setData(Uri.parse("testmail#gmail.com"));
send.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
startActivity(send);
}
catch (ActivityNotFoundException ex)
{
ex.printStackTrace();
}
}
});
Finally i Got the answer,it was really hectic for that piece of code to make that work
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/html");
final PackageManager pm = getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);
ResolveInfo best = null;
for (final ResolveInfo info : matches) {
if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail")) {
best = info;
break;
}
}
if (best != null) {
intent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
}
intent.setData(Uri.parse("mailto:emailto#gmail.com"));
try {
startActivity(intent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "Error Sending Email,Try Later.", Toast.LENGTH_SHORT).show();
}
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();
}
}
});
}
}
I am developing an application.
In that I am trying to share information of my application as a Status of Facebook and Twitter when button is clicked.
When I click on twitter button the status as I want to share is twitted automatically but In facebook I cant share info as I want in show blank textbox of facebook share status window.
Where I am wrong please tell me ?
This is my code.
final String msg="Hie - " +
"Message to share";
#Override
protected void onCreate(Bundle savedInstanceState)
{
.....................
.....................
static SocialAuthAdapter adapter;
adapter = new SocialAuthAdapter(new ResponseListener());
try
{
adapter.addConfig(Provider.TWITTER, "key", "BsiY4LH5Y4naSmb.............",null);
}
catch (Exception e)
{
e.printStackTrace();
}
imgtellfriend.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
anim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.sequence);
imgtellfriend.startAnimation(anim);
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, msg);
sendIntent.setType("text/plain");
startActivity(sendIntent);
}
});
imgFb.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
anim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.sequence);
imgFb.startAnimation(anim);
adapter.authorize(Catagories.this, Provider.FACEBOOK);
initShareIntent("com.facebook.katana");
}
});
imgtwiter.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
anim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.sequence);
imgtwiter.startAnimation(anim);
adapter.authorize(Catagories.this, Provider.TWITTER);
}
});
}
private final class ResponseListener implements DialogListener
{
#Override
public void onComplete(Bundle values)
{
Log.d("Share-Bar", "Authentication Successful");
final String providerName = values.getString(SocialAuthAdapter.PROVIDER);
Log.d("Share-Bar", "Provider Name = " + providerName);
Toast.makeText(Catagories.this, providerName + " connected", Toast.LENGTH_SHORT).show();
adapter.updateStatus(msg, new MessageListener(), true);
}
#Override
public void onCancel()
{
Log.d("Share-Bar", "Authentication Cancelled");
}
#Override
public void onBack()
{
Log.d("Share-Bar", "Dialog Closed by pressing Back Key");
}
#Override
public void onError(SocialAuthError error)
{
initShareIntent("com.facebook.katana");
error.printStackTrace();
Log.d("Share-Bar 1", error.toString());
Log.d("Share-Bar 2", error.getMessage());
}
}
#SuppressLint("DefaultLocale")
private void initShareIntent(String type)
{
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 = getPackageManager().queryIntentActivities(share, 0);
if (!resInfo.isEmpty()){
for (ResolveInfo info : resInfo) {
Log.d("MEssage FB","--------- App :: "+info.activityInfo.packageName.toLowerCase());
if (info.activityInfo.packageName.toLowerCase().contains(type) ||
info.activityInfo.name.toLowerCase().contains(type) )
{
share.putExtra(Intent.EXTRA_SUBJECT, "ECard");
share.putExtra(Intent.EXTRA_TEXT, msg);
share.setPackage(info.activityInfo.packageName);
found = true;
break;
}
}
if (!found)
return;
startActivity(Intent.createChooser(share, "Select"));
}
}
private class MessageListener implements SocialAuthListener<Integer>
{
#Override
public void onExecute(String provider, Integer t)
{
Integer status = t;
if (status.intValue() == 200 || status.intValue() == 201 || status.intValue() == 204)
Toast.makeText(Catagories.this, "Message posted on " + provider, Toast.LENGTH_LONG).show();
else
Toast.makeText(Catagories.this, "Message not posted on " + provider, Toast.LENGTH_LONG).show();
}
#Override
public void onError(SocialAuthError e) {
}
I am using socialauth library for that.
SocialAUth Android new version is working for facebook. The only thing is you need to use native facebook android login.
socialauth library uses old facebook sdk.
dont use that.
you can directly use intent filter to share on social networks.
[Updated one]
check this
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/png");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"YOUR TEXT HERE");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,"YOUR TEXT HERE");
shareIntent.putExtra(android.content.Intent.EXTRA_STREAM,pngUri);
PackageManager pm = getApplicationContext().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);
startActivity(shareIntent);
}
}
}
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.
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