I have started my final year android project and I was wondering when the user presses a button how could you open, for example, the Facebook app and if the user does not have the Facebook app installed they will be redirected to m.facebook.com in their browser?
Asked
Active
Viewed 255 times
0
-
Just prepare an intent with the web url. If the facebook app is installed, the user will be asked if she wants to use the app or the browser to open it. – Henry Sep 26 '15 at 05:31
2 Answers
0
You can use Try/Catch to handle it. if Facebook app was installed, fb://page/123 link will open in app. Else, Catch code will run.
@Override
public void onClick(View v) {
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri
.parse("fb://page/12345"))); //12345 is Facebook page number
} catch (Exception e) {
//open link in browser
startActivity(new Intent(Intent.ACTION_VIEW, Uri
.parse("http://m.facebook.com/etc")));
}
}

Milad Nouri
- 1,587
- 1
- 21
- 32
-
This is what I have done and it still seems to open the URL even though I have the app installed. ` try { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("com.webmd.android"))); } catch (Exception e) { //open link in browser startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://symptoms.webmd.com/default.htm#introView"))); }` – seamus911 Sep 26 '15 at 06:38
0
You can check whether a package(facebook-app) is installed or not in device from here and here, if it is installed you can Intent
the Uri
else Intent to Activity
contains WebView
Well I haven't used facebook intent but I have use skype in my project You can use ActivityNotFoundException
not found Exception
change skype uri with facebook one eg..(facebook://facebook.com/inbox)
@Override
public void onClick(View v) {
try {
Intent myIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("skype:"+skype+"?call")); // place your facebook uri here
startActivity(myIntent);
} catch (ActivityNotFoundException e) {
//--- intent your url here which will be redirected to webview if facebook app is not installed..
}
}
-
This is what I have done and it still seems to open the URL even though I have the app installed. `try { Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("com.webmd.android")); // place your facebook uri here startActivity(myIntent); } catch (Exception e) { //open link in browser startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://symptoms.webmd.com/default.htm#introView"))); }` – seamus911 Sep 26 '15 at 06:34
-