I want to open an Facebook link from my android application.
The URL is looks like http://www.facebbok.com/abcxyz. It should open the 'abcxyz' page in the Facebook application, but it is always opening in browser.
Code:
try
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activityContext.startActivity(browserIntent);
}
catch (ActivityNotFoundException ex)
{
ex.printStackTrace();
}
My android OS version is 6.0.1.
I have the same issue with Instagram, http://www.instagram.com/abcxyz, while other applications like Youtube work.
You should use facebook's custom url scheme to force app to open your page like below:
public Intent getFacebookIntent(String url) {
PackageManager pm = context.getPackageManager();
Uri uri = Uri.parse(url);
try {
ApplicationInfo applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
if (applicationInfo.enabled) {
uri = Uri.parse("fb://facewebmodal/f?href=" + url);
}
}
catch (PackageManager.NameNotFoundException ignored) {
}
return new Intent(Intent.ACTION_VIEW, uri);
}
Related
I want to open a page in fb app using a deep link (on.fb.me/xxx) but it opens in the web browser, is there a way I can do this? or Am I forced to use the usual fb url (fb://xxx).
the code I'm using right now
final Uri uri = Uri.parse("http://on.fb.me/xxx");
final Intent facebookIntent = new Intent(Intent.ACTION_VIEW, uri);
facebookIntent.setPackage("com.facebook.katana");
try {
startActivity(facebookIntent);
} catch (ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
I have the same issue with Instagram and Twitter with links like "bit.ly/xxx"
I just linked some social networks to my app as a preliminary test and using the same kind of code, the results I am getting are different for Facebook, Instagram and Twitter intents.
When I click Facebook or Twitter, it opens the app automatically when it's installed and uses browser when it's not. However, that's not the case with Instagram. The complete action using dialog pops up and that's not something I want to happen.
protected void LaunchInstagram() {
String InstagramUsername = "USERNAME";
String LaunchInstagram = "http://instagram.com/_u/" + InstagramUsername;
String InstagramURL = "https://instagram.com/" + InstagramUsername;
try {
this.getPackageManager().getPackageInfo("com.instagram.android", 0);
Uri Uri = Uri.parse(LaunchInstagram);
startActivity(new Intent(Intent.ACTION_VIEW, Uri));
} catch (PackageManager.NameNotFoundException InstagramAppNotFoundOpenBrowser) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(InstagramURL)));
}
}
You are using
String InstagramURL = "https://instagram.com/" + InstagramUsername;
So every Intent you'll try to launch, will open a chooser because this is a URL.
than every App that supports URL, like browsers and such, will try to handle that.
EDIT 1:
Try it this way:
public static boolean openApp(Context context, String packageName) {
PackageManager manager = context.getPackageManager();
try {
Intent i = manager.getLaunchIntentForPackage(packageName);
if (i == null) {
return false;
//throw new PackageManager.NameNotFoundException();
}
i.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(i);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
I'm not sure if that will help you to open a specific user page on the App thou.
EDIT 2:
Try it this way:
Uri uri = Uri.parse("http://instagram.com/_u/USERNAME");
Intent insta = new Intent(Intent.ACTION_VIEW, uri);
insta.setPackage("com.instagram.android");
startActivity(insta);
I know is a well-known topic, but none of the solutions worked for me.
I'm using this code to launch facebook app at a specific profile page from my app:
try {
Intent fb_intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/" + id_facebook));
fb_intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(fb_intent);
} catch (Exception e) {
Intent fb_intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/" + id_facebook));
fb_intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(fb_intent);
}
Facebook App opens correctly but shows an infinite loop with no data at all on the requested profile page.
Any idea why? Thank a lot in advance
I try to figure out with similar trouble..
public static Intent newFacebookIntent(PackageManager pm, String userId) {
Uri uri;
try {
pm.getPackageInfo("com.facebook.katana", 0);
uri = Uri.parse("fb://facewebmodal/f?href=" + userId);
} catch (PackageManager.NameNotFoundException e) {
uri = Uri.parse(userId);
}
return new Intent(Intent.ACTION_VIEW, uri);
}
For it's work fine
https://stackoverflow.com/a/24547478/3864698
I am trying to open certain Facebook profile page in my android app with specific user ID.
My code opens facebook app if it is installed otherwise open webbrowser.
It works fine except when facebook app is installed and it is closed.
In that situation it just opens newsfeed page instead of the profile page. When the facebook app is open at background, it successfully redirect to desired profile page. How can I solve this ? Also is there an official facebook document which describes about the way to access facebook app URI ?
try{
this.getPackageManager()
.getPackageInfo("com.facebook.katana", 0); //Checks if FB is even installed.
final String facebookScheme = String.format("fb://profile/%s", user2FbID);
final Intent facebookIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(facebookScheme));
facebookIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY
| Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(facebookIntent);
}catch (Exception e) {
String facebookScheme = "https://m.facebook.com/" + user2FbID;
Intent facebookIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(facebookScheme));
startActivity(facebookIntent);
}
Try this, Works for me
try
{
Intent followIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/<your profile_id>"));
startActivity(followIntent);
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run() {
Intent followIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/<your profile_id>"));
startActivity(followIntent);
}
}, 1000 * 2);
}
catch (Exception e)
{
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/<user_name>")));
String errorMessage = (e.getMessage()==null)?"Message is empty":e.getMessage();
Log.e("Unlock_ScreenActivity:FacebookAppNotFound" ,errorMessage);
}
This was happening with me too. Updated the Facebook app and it's solved now, seems it was something to do in with Facebook. It's working properly now.
Here's the code I'm using:
public static Intent getOpenFacebookIntent(Context context) {
try{
// open in Facebook app
context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/<profile_id>"));
} catch (Exception e) {
// open in browser
return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/<profile_id>"));
}
}
I was looking for some way to launch Twitter app and open a specified page from my application, without webview.
I found the solution for Facebook here:
Opening facebook app on specified profile page
I need something similar.
[EDIT]
I've just found a solution:
try {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("twitter://user?screen_name=[user_name]"));
startActivity(intent);
} catch (ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://twitter.com/#!/[user_name]")));
}
Based on fg.radigales answer, this is what I used to launch the app if possible, but fall back to the browser otherwise:
Intent intent = null;
try {
// get the Twitter app if possible
this.getPackageManager().getPackageInfo("com.twitter.android", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=USERID"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} catch (Exception e) {
// no Twitter app, revert to browser
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/PROFILENAME"));
}
this.startActivity(intent);
UPDATE
Added intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); to fix an issue where twitter was opening inside my app instead of as a new activity.
This worked for me: twitter://user?user_id=id_num
Open page on Twitter app from other app using Android in 2 Steps:
1.Just paste the below code (on button click or anywhere you need)
Intent intent = null;
try{
// Get Twitter app
this.getPackageManager().getPackageInfo("com.twitter.android", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=USER_ID"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} catch () {
// If no Twitter app found, open on browser
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/USERNAME"));
}
2.intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=USER_ID"));
To get USER_ID just write username https://tweeterid.com/ and get Twitter User ID in there
Reference: https://solutionspirit.com/open-page-twitter-application-android/
My answer builds on top of the widely-accepted answers from fg.radigales and Harry.
If the user has Twitter installed but disabled (for example by using App Quarantine), this method will not work. The intent for the Twitter app will be selected but it will not be able to process it as it is disabled.
Instead of:
getPackageManager().getPackageInfo("com.twitter.android", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=2343965036"));
You can use the following to decide what to do:
PackageInfo info = getPackageManager().getPackageInfo("com.twitter.android", 0);
if(info.applicationInfo.enabled)
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=2343965036"));
else
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/wrkoutapp"));
Just try this code snippet. It will help you.
//Checking If the app is installed, according to the package name
Intent intent = new Intent();
intent.setType("text/plain");
intent.setAction(Intent.ACTION_SEND);
final PackageManager packageManager = getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : list)
{
String packageName = resolveInfo.activityInfo.packageName;
//In case that the app is installed, lunch it.
if (packageName != null && packageName.equals("com.twitter.android"))
{
try
{
String formattedTwitterAddress = "twitter://user/" ;
Intent browseTwitter = new Intent(Intent.ACTION_VIEW, Uri.parse(formattedTwitterAddress));
long twitterId = <Here is the place for the twitter id>
browseTwitter.putExtra("user_id", twitterId);
startActivity(browseTwitter);
return;
}
catch (Exception e)
{
}
}
}
//If it gets here it means that the twitter app is not installed. Therefor, lunch the browser.
try
{
String twitterName = <Put the twitter name here>
String formattedTwitterAddress = "http://twitter.com/" + twitterName;
Intent browseTwitter = new Intent(Intent.ACTION_VIEW, Uri.parse(formattedTwitterAddress));
startActivity(browseTwitter);
}
catch (Exception e)
{
}
For me this did the trick it opens Twitter app if you have it or goes to web browser:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/"+"USERID"));
startActivity(intent);
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]")));
}
This answer was posted as an edit to the question Open page in Twitter app from other app - Android by the OP jbc25 under CC BY-SA 3.0.