How to Share Our Own Static Status in Facebook using Android - android

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

Related

App is Crashing while opening gmail from android app on clicking a button

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();
}

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

ShareDialog doesn't open facebook app

Trying to implement facebook sharing, however it only works in webview if I set ShareDialog.Mode.AUTOMATIC, if I set it to NATIVE then nothing shows up at all and I don't receive no error callback. Facebook app is installed in the emulator.
Provider in manifest:
<provider android:authorities="com.facebook.app.FacebookContentProvider{myAppId}"
android:name="com.facebook.FacebookContentProvider"
android:exported="true"/>
Code for sharing:
public void share(String contentUrl,
Activity activity){
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse(contentUrl))
.build();
ShareDialog dialog = new ShareDialog(activity);
dialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
EventBus.getDefault().post(new ShareEvent(true));
}
#Override
public void onCancel() {
Log.e(TAG, "Facebook Share Cancel");
EventBus.getDefault().post(new ShareEvent(false));
}
#Override
public void onError(FacebookException error) {
Log.e(TAG, "Facebook share error: " + error.getMessage());
EventBus.getDefault().post(new ShareEvent(false));
}
})
dialog.show(content, ShareDialog.Mode.NATIVE);
}
I'm calling share(..) method from fragment.
I tried implementing sharing using intents, and that works
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://myUrl.com");
PackageManager pm = activity.getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
if ((app.activityInfo.name).contains("facebook")) {
final ActivityInfo mActivity = app.activityInfo;
final ComponentName name = new ComponentName(mActivity.applicationInfo.packageName, mActivity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
activity.startActivity(shareIntent);
break;
}
}
However in my case I need successful share callback, which as much as I know my last solution doesn't provide, since it always return result CANCELED
Have you declared permission for it? Like below:
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
List<String> permissionNeeds = Arrays.asList("publish_actions"); // permission.
loginManager = LoginManager.getInstance();
loginManager.logInWithPublishPermissions(this, permissionNeeds);
loginManager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// do something here
finish();
}
#Override
public void onCancel() {}
#Override
public void onError(FacebookException exception) {}
});
This is my sample code: https://github.com/minhhuy150894/simple-puzzle/blob/master/app/src/main/java/xyz/davidng/puzzle/FacebookShareImage.java

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();
}
}
});
}
}

choosing certain apps to appear in the share chooser

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.

Categories

Resources