App force closes when google+ is not installed - android

My requirement is to share on Social networking sites. So, I have already done with Facebook and Twitter. But I am stuck with Google+. I have the following code to share on Google+, but the app forcecloses when I start activity. This occurs only when the Google+ app isn't installed on device already. I know this sharing intent requires the Google+ already installed to start the activity.
Now what I need to do is at least to inform the user that the google+ sharing requires already installed google+ app through dialog or toast instead of getting force closed (If possible by clicking on ok on the dialog should redirect to the google+ on google play). If the google+ app is installed already it goes as usual.
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setText("Hello there! This is a pic of the lazy cat")
.setType("image/jpeg")
.setStream(Uri.parse(path))
.getIntent()
.setPackage("com.google.android.apps.plus");
startActivity(shareIntent);
Any help is appreciated. Thanks in advance.

UPDATE
The answer below is outdated. You can now check if the Google+ app is installed through the Google Play Services library (available through the Android SDK). See here for information how to add it to your project.
Example:
int errorCode = GooglePlusUtil.checkGooglePlusApp(mContext);
if (errorCode != GooglePlusUtil.SUCCESS) {
//Google+ is either not present or another error occured, show the error dialog
GooglePlusUtil.getErrorDialog(errorCode, this, 0).show();
}
else{
//Your Google+ related code here
}
OLD ANSWER
You can create some sort of check to see if the Google+ app is installed:
public void loadGooglePlus()
{
if(isGooglePlusInstalled())
{
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setText("Hello there! This is a pic of the lazy cat")
.setType("image/jpeg")
.setStream(Uri.parse(path))
.getIntent()
.setPackage("com.google.android.apps.plus");
startActivity(shareIntent);
}
else{
//Notify user
}
}
public boolean isGooglePlusInstalled()
{
try
{
getPackageManager().getApplicationInfo("com.google.android.apps.plus", 0 );
return true;
}
catch(PackageManager.NameNotFoundException e)
{
return false;
}
}

Related

Android Intent to launch Google+ app at Google+ Community screen

There is already a good SO question for displaying a Google+ Page in the Google+ Android app:
Open Google Plus Page Via Intent In Android
But what about the Intent to launch the Google+ app at a specific Google+ Community?
EDIT - to the silent down-voters, please explain why you down-voted.
My solution, working with G+ version "5.3.0.91034052", tested today
final Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( "https://plus.google.com/communities/107847486351510098159" ) );
intent.setPackage( "com.google.android.apps.plus" );
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity( intent );
}
It's not bullet proof, you need Google+ app on your device, in case you don't have it some kind of try-catch might be nice (?),
I achieved this by using:
String communityPage = "communities/123456789";
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.google.android.apps.plus",
"com.google.android.apps.plus.phone.UrlGatewayActivity");
intent.putExtra("customAppUri", communityPage);
startActivity(intent);
} catch(ActivityNotFoundException e) {
// fallback if G+ app is not installed
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://plus.google.com/"+communityPage)));
}
Where 123456789 is the id of the community copied from the address bar.
Just in case anyone else needs to do this I had to do this in my app and used the below code
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://plus.google.com/communities/1234356789")));
Where 1234356789 is the community id from the address bar.
This then prompts whether you want to open with Google+ or browser.

Android Facebook (Something went wrong) Share Dialog

i am using the facebook sdk 3.5 to implement sharing dialog i can now successfully show a share dialog with link app name and description it now goes to facebook ...however when i press the share button it posts a message on toast with:
something went wrong please try again
Here is how i use share dialog:
FacebookDialog.ShareDialogBuilder shareDialogBuilder = new FacebookDialog.ShareDialogBuilder(MyClass.this)
.setApplicationName("app name")
.setName("Name")
.setLink("my link");
if (shareDialogBuilder.canPresent()) {
shareDialogBuilder.build().present();
} else {
Log.v("", "CAN_PRESENT() failed");
}
i read some related issues and checked on my link but it is working properly and called properly.. is there any way that i can log the error? what other issues might be causing this issue?
thank you.

Sharing Content On FaceBook Android

I use intent and Action.SEND for sharing my custom message on social networks like WhatsApp , twitter, Facebook and GMail. Everything is ok on Gmail and other applications except Facebook! How can I customize my code to share something on Facebook as well? I do share on Facebook using Facebook SDK with no problem, but I want to do it using an intent.
this is what I use:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, knowTitle+"Read the full article via MomsApp by EnfaMama A+ at http://meadjohnsonasia.com.my/mobileapp");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "I just read "+knowTitle);
sendIntent.setType("*/*");
startActivity(Intent.createChooser(sendIntent, "Share Your Favorite Article"));
What I did was actually to intercept the chosen target of the intenthandlers, you can do that by using your actionprovider. Let's say you created an item that with an onclick starts the intent. In order to do that, you can instantiate an actionprovider to do so. This actionprovider can have a setOnShareTargetSelectedListener to intercept any intents that you want to handle differently (or not at all ^^). See the code below for how to configure your actionprovider.
actionProvider.setShareIntent(createShareIntent());
actionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener(){
#Override
public boolean onShareTargetSelected(ShareActionProvider source,
Intent intent) {
if ("com.facebook.katana".equals(intent.getComponent().getPackageName()) && mfacebooksharer != null) {
mfacebooksharer.shareStatus(subject, text);
return true;
}
return false;
}
});
Whenever facebook is chosen, I use my mfacebooksharer to handle the intent and follow the facebook API.
Ofcourse, that actionrpovider needs to have an intent. (Just like you wanted to work with an intent). I use the method below to create the intent.
private Intent createShareIntent() {
intentsetter.setIntentleave(true);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);
return shareIntent;
}
As per the Facebook's Platform Policies, you cannot pre-fill the share dialog using
Intent.EXTRA_TEXT. It is usually thought to be a bug, but as per a Bug Report filed here and also, here, Facebook clearly mentions that this is not the case (it's not a bug).
You can read more about their Platform Policies specifically, Platform Policy IV.2
Quote from Platform Policy IV.2:
You must not pre-fill any of the fields associated with the following
products, unless the user manually generated the content earlier in
the workflow: Stream stories (user_message parameter for
Facebook.streamPublish and FB.Connect.streamPublish, and message
parameter for stream.publish), Photos (caption), Videos (description),
Notes (title and content), Links (comment), and Jabber/XMPP.
These fields are intended for users to express themselves. Pre-filling
these fields erodes the authenticity of the user voice.
The only way you can share stories from your App is by integrating the Facebook SDK, which as per your post, you are already able to successfully. That is the only option available (unfortunately).
Using Intent in Android, you can share only a link without text:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "http://www.google.ca");
startActivity(Intent.createChooser(intent, "Share with"));
It'll work. If you want to share text and link , you have to use the Facebook SDK for Android: https://github.com/facebook/facebook-android-sdk

Open page in Facebook,Twitter and Google Plus app from other app - Android

I'm working on an application where I need to integrate the social functionality of the different social networks: Facebook, Twitter, Google+.
For now, in Facebook and Twitter i'm recognized if the user has a native application and if he does, I'm opening it and show him my fan page.
For Twitter I use the next code:
try {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("twitter://user?screen_name=[user_name]"));
startActivity(intent);
}catch (Exception e) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://twitter.com/#!/[user_name]")));
}
And for Facebook the next code:
try{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/" + PROFILE_FACEBOOK_APP_ID));
startActivity(intent);
}catch(Exception e){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/UserNamePage")));
}
Now I want to do the same thing for Google+. I saw that I can browse to my fan page with the next Url https://plus.google.com/MY_PAGE_ID/, but it keep asking me if I want to open it with Google+ application or with the browser, and I want that he will open it with the application automatically, without asking the user.
Is there a simple way to do this?
Thanks.
Found a solution:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.google.android.apps.plus",
"com.google.android.apps.plus.phone.UrlGatewayActivity");
intent.putExtra("customAppUri", "FAN_PAGE_ID");
startActivity(intent);
I think this is quite safe, because we do not need to specify the component, just the google+ app package name:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://plus.google.com/[Google+ID]/"));
intent.setPackage("com.google.android.apps.plus"); // don't open the browser, make sure it opens in Google+ app
startActivity(intent);
Unknown if google plus needs some other information in the Intent but as general Android solution you can explicitly set the target. You will need the package name of google+.
More info here: http://developer.android.com/reference/android/content/Intent.html#setPackage%28java.lang.String%29
For example:
Intent.setPackage("com.google.android.apps.plus"); //Don't know the exact package name

Share via Google+..dialog complete action with shown

hi everybody i've a little problem..i'm trying to share a text using google plus app, but the lastest version give me a problem.
when i call with an intent "com.google.android.apps.plus", android display me this dialog
so if i choose the first one, text will be shared correctly, but the second one do nothing.
if the app is not intalled i redirect the user to the market, at g+ download page (this work fine)
if (v == plus) {
social(2);
targetedShareIntent.putExtra(
android.content.Intent.EXTRA_TEXT, user);
targetedShareIntent
.setPackage("com.google.android.apps.plus");
startActivity(targetedShareIntent);
}
"v" is a button and social check if app is installed
any sugestion?
both options open google+ application?
I suppose show two options because google+ app have two activities with category.LAUNCHER.... the app and the chat app
PSDT: sorry for my bad english!
I was able to get the G+ dialog to launch with the following code (after querying the package manager to make sure the app was actually installed and passing in the currentContext as a parameter):
Intent appIntent = new Intent(Intent.ACTION_SEND);
String shareText = "Share text goes here";
appIntent.setType("text/plain");
appIntent.putExtra(Intent.EXTRA_TEXT, shareText);
//Filters so only the G+ app will launch
appIntent.setPackage("com.google.android.apps.plus");
try {
currentContext.startActivity(appIntent);
} catch (android.content.ActivityNotFoundException e) {
Log.d(e.getMessage());
}
Drove me bananas figuring this out, so I figured I'd try to save someone else the hassle.

Categories

Resources