'How to open Android App or URL on click inside app [duplicate]
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?
Solution 1:[1]
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")));
}
}
Solution 2:[2]
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..
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Milad Nouri |
Solution 2 | Community |