Opening Facebook app with another ? With the "Twitter way" - android

My app needs to display a Facebook account. So I would like the Facebook application opens or if it is not available, the web browser. Currently I can open the Twitter app with this way and the system ask what to do (Twitter app or the browser).
Here is the code:
WebView myWebView = (WebView) findViewById(R.id.webViewProfile); // Create an instance of WebView and set it to the layout component created with id webview in main.xml
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("https://mobile.twitter.com/"+name); // Specify the URL to load when the application starts
myWebView.setInitialScale(1); // Set the initial zoom scale
myWebView.getSettings().setBuiltInZoomControls(true); // Initialize zoom controls for your WebView component
myWebView.getSettings().setUseWideViewPort(true); // Initializes double-tap zoom control
myWebView.getSettings().setUserAgentString("BumpMe");
This method is very good and I want to have the same result for Facebook. Currently I can open the Facebook app like that:
String uri = "facebook://facebook.com/info?user="+fbid;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
context.startActivity(intent);
But if the application is not installed it won't work...
How can I do it like for twitter ?
Thanks

The reason this works with Twitter is that the Twitter app handles https://mobile.twitter.com/* URL's in its intent filters; so when you issue an intent that has that URL, the Twitter app gets called. So the only way to do this with Facebook (or any other app) would be to use a URL that matches one of the patterns declared in its intent filters. Maybe http://m.facebook.com/... ?

Instead of offering the user the choice of app or browser, another way is to try to open the app, and if that fails to open the web page. Here is how I do that for Twitter:
long userID = tweet.getUser().getId();
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=" + userID));
startActivity(intent);
}catch (Exception e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/intent/user?user_id=" + userID)));
}

Related

How to open the Amazon AppStore directly from my Android application?

I have seen answers regarding Amazon Appstore from iPhone but not from Android. I'm trying to make a button that will open the Amazon Appstore (for android) on my app's page.
Here is how it's done for Google Play:
final String appPackageName = context.getPackageName();
try {
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
} catch (android.content.ActivityNotFoundException e) {
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
}
How should I replace the Strings so that it works "the same" for the Amazon AppStore?
The Amazon app store URI is
amzn://apps/android?
Directing to a particular app would look like
amzn://apps/android?p=com.amazon.mp3
Source: Linking To the Amazon Appstore for Android
Look into WebViews. It does exactly what you want, you can open a page in your own app. Simply define a webview in the xml, and use the java to display the page.
How to do it without webview:
(From docs)
By default, a WebView provides no browser-like widgets, does not
enable JavaScript and web page errors are ignored. If your goal is
only to display some HTML as a part of your UI, this is probably fine;
the user won't need to interact with the web page beyond reading it,
and the web page won't need to interact with the user. If you actually
want a full-blown web browser, then you probably want to invoke the
Browser application with a URL Intent rather than show it with a
WebView. For example:
Uri uri = Uri.parse("http://www.example.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
With webview:
webview.loadUrl("http://slashdot.org/");
// OR, you can also load from an HTML string:
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);
Let me know if this works by commenting.

Open Facebook page via Facebook app

How can I open Facebook page using insalled Facebook app? Exactly page, not profile, because fb://profile works fine.
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/page_id")));
Seems like fb://page isn't working, because it's just opens feed.
fb://page/{id} is the way to go:
final String url = "fb://page/" + facebookID
Intent facebookAppIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
facebookAppIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(facebookAppIntent);
Are you sure you are using the ID number of your Facebook page and NOT the username?
What I mean is that if, for example, you want to open the Facebook page of Facebook itself (i.e. https://www.facebook.com/facebook) you have to use:
fb://page/20531316728
Since 20531316728 is the ID of the Facebook page (while facebook is the username).
If you don't know the ID of your Facebook page, you can retrieve it opening:
https://graph.facebook.com/{username}
Make Sure That You are Using the page id of your page correctly
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse("fb://page/YOUR PAGE ID"));
startActivity(intent);
i think u should use
https://www.facebook.com/
instead of fb://page/ or fb://profile or something.
I hope it's helpful!

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

Launch custom Android application from WebView

I have an HTML file which is launching an app if I open it in the Android native browser,
but when I try to open the same in a WebView, it is not able to launch that application, and "Webpage not available" is shown. I think my WebView is not able to handle the scheme "my.special.scheme://" defined for the application.
I read Launching an Android Application from the Browser, but it does not cover information about launching an app from a WebView.
It's true, links with a custom URI scheme don't load automatically launch apps from a WebView.
What you need to do is add a custom WebViewClient to your WebView:
webView.setWebViewClient(new CustomWebViewClient());
and then in the shouldOverrideUrlLoading(), have the following code:
public boolean shouldOverrideUrlLoading(final WebView webView, final String url) {
if (url.startsWith("my.special.scheme://")) {
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
// The following flags launch the app outside the current app
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
return true;
}
return false;
}
I'm not sure, but I believe that WebView simply doesn't handle custom URI schemes.
The workaround is to override WebViewClient.shouldOverrideUrlLoading() and manually test if the URL uses your URI scheme, launching your app and returning true if it matches, otherwise returning false.
I perform an ACTION_VIEW with the URL to make the URL open in the default browser, which will do the redirecting to the concerning app (I had to fix this concerning payments via a bank app)

Android - Call default browser with and redirect to a designated url

Hi
I want to write an app to call default browser and redirect to a designated url.
Any suggestion to 1)call the default browser, 2)redirect to a designated url.
Thanks
you just want to launch an ACTION_VIEW intent with the Uri of the webpage as your data element :
Intent httpIntent = new Intent(Intent.ACTION_VIEW);
httpIntent.setData(Uri.parse("http://www.bbc.co.uk"));
startActivity(httpIntent);
To open the default browser use an Intent with the action VIEW. To tell the browser which page to load us the data-part of the Intent.
Example:
Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse("http://stackoverflow.com"));
startActivity(browse);
Since this is a basic task in Android you might want to read some basics about Intents in Android.

Categories

Resources