How to open Facebook and Twitter in android - android

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/html");
String str = et.getText().toString();
i.putExtra(android.content.Intent.EXTRA_TEXT,str);
startActivity(Intent.createChooser(i,"Share using"));
I can't open Facebook and Twitter with this code in Android but other applications like Gmail and Skype are opened successfully. Why's that? And how do I open Facebook and Twitter?

Can you try this code:
private void facebooktaPaylas() {
try {
String facebookUri = "http://m.facebook.com/sharer.php?u=";
String marketUri = "your sharing text");
Intent shareOnFacebookIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(facebookUri + marketUri));
startActivity(shareOnFacebookIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
private void twitterdaPaylas() {
try {
String facebookUri = "http://mobile.twitter.com/home?status=";
String marketUri = "your sharing text";
Intent shareOnFacebookIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(facebookUri + marketUri));
startActivity(shareOnFacebookIntent);
} catch (Exception e) {
e.printStackTrace();
}
}

How about changing the MIME type to "text/*"?
It seems that Facebook and Twitter can deal with "text/plain" instead of "text/html".

Related

Open User Page in Tiktok app on Android with Intent

Is there something similar to the Soundcloud Uri ("soundcloud://users:" + "soundcloud_username") that allows an app user to open the Tiktok app and go to another user's page, like the Soundcloud code below?
Soundcloud code reference: Force a Soundcloud track to open in Soundcloud app on Android
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("soundcloud://users:" + "soundcloud_username"));
startActivity(intent);
} catch (ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://soundcloud.com/"+string_soundcloudLink_profile)));
}
private fun openTikTokProfile() {
val uri: Uri = Uri.parse("https://www.tiktok.com/#${username}")
val intent = Intent(Intent.ACTION_VIEW, uri)
intent.setPackage("com.zhiliaoapp.musically")
if (intent.resolveActivity(requireActivity().packageManager) != null) {
startActivity(intent)
}
}
Hey I found the solution But its in xamarin.android
private void urlclick(object sender, EventArgs e)
{
PackageManager manager = PackageManager;
var U =Android.Net.Uri.Parse("http://vm.tiktok.com/tiktokulr");///Add URL of you tiktok account
Intent i;
Intent icheck;
try
{
i = new Intent(Intent.ActionView, U);
icheck = manager.GetLaunchIntentForPackage("com.zhiliaoapp.musically");
if (icheck == null)
{
Toast.MakeText(this, "please install tiktok in your phone", ToastLength.Short).Show();
throw new PackageManager.NameNotFoundException();
}
else
{
StartActivity(i);
}
}
catch (PackageManager.NameNotFoundException exp)
{
}

How can I launch Instagram app on button click Android Studio

I just want to open Instagram application on button click (if installed) on minimum API 16.
What I am trying is:
Intent likeIng = new Intent(Intent.ACTION_VIEW);
likeIng.setPackage("com.instagram.android");
try {
startActivity(likeIng);
} catch (ActivityNotFoundException e) {
Toast.makeText(this,"Instagram Not Installed!",Toast.LENGTH_LONG).show();
}
But when running on a phone where Instagram is installed, it's not launching it.
pass this this Uri to your intent.
Uri uri = Uri.parse("http://instagram.com/_u/YOUR_USERNAME");
Intent i= new Intent(Intent.ACTION_VIEW,uri);
i.setPackage("com.instagram.android");
try {
startActivity(i);
} catch (ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://instagram.com/xxx")));
}
Use PackageManager.getLaunchIntentForPackage(String packageName)
use this. I already used this.
Intent i = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
startActivity(i);
Try this method:
private void callInstagram() {
String apppackage = "com.instagram.android";
Context cx=this;
try {
Intent i = cx.getPackageManager().getLaunchIntentForPackage(apppackage);
cx.startActivity(i);
} catch (Exception e) {
Toast.makeText(this, "Sorry, Instagram Apps Not Found", Toast.LENGTH_LONG).show();
}
}

Share Image and data to Twitter in android

I ma developing one application in i have to share data and image to Twitter from my android Application.I am following http://chintankhetiya.wordpress.com/2013/07/18/sharing-text-image-in-twitter-android-example/comment-page-1/#comment-285 this.I did change consumer key and consumer secret .While Running my App one Toast message coming Login Faild .Please tell me any thing required for this
For twitter sharing you can share file directly from the twitter app if it is install and if the app is not install you can use twitter api.
Below is the code for both the things
Boolean isTwitterAppAvailable =false;
try {
Intent shareIntent = new Intent(
android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"Content to share");
PackageManager pm = getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(
shareIntent, 0);
for (final ResolveInfo app : activityList) {
if (app.activityInfo.name.contains("twitter")) {
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(
activity.applicationInfo.packageName,
activity.name);
shareIntent.setClassName("com.twitter.android",
"com.twitter.android.PostActivity");
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
shareIntent.putExtra(Intent.EXTRA_TEXT,
GlobalApplication.twitter_share_msg);
if (GlobalApplication.isImageShareTwitter)
shareIntent.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file:///sdcard/attachment.jpg"));
isTwitterAppAvailable = true;
startActivity(shareIntent);
break;
}
}
if (!isTwitterAppAvailable) {
if (isNetworkAvailable()) {
GlobalApplication.casted_image = casted_image;
// twitter web sharing using twitter4j api...
Twitt_Sharing twitt = new Twitt_Sharing(
ShowCampaignDetail.this, twitter_consumer_key,
twitter_secret_key);
twitt.shareToTwitter(
GlobalApplication.twitter_share_msg,
casted_image);
Twitter_Handler tHandler = new Twitter_Handler(
ShowCampaignDetail.this, twitter_consumer_key,
twitter_secret_key);
User user;
try {
user = tHandler.twitterObj
.showUser(tHandler.twitterObj.getId());
String url = user.getProfileImageURL();
} catch (IllegalStateException e) { // TODO
// Auto-generated
e.printStackTrace();
} catch (TwitterException e) {
e.printStackTrace();
}
} else {
Toast.makeText(ShowCampaignDetail.this,
"No Network Connection Available !!!",
Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
Use Following code :
/*
* Share any Image.
*/
private void shareImage() {
Intent share = new Intent(Intent.ACTION_SEND);
// If you want to share a png image only, you can do:
// setType("image/png"); OR for jpeg: setType("image/jpeg");
share.setType("image/*");
// Make sure you put example png image named myImage.png in your
// directory
String imagePath = Environment.getExternalStorageDirectory()
+ "/myImage.png";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share image to..."));
}

How can I post on Twitter with Intent Action_send?

I have been struggling to send text from my app to Twitter.
The code below works to bring up a list of apps such as Bluetooth, Gmail, Facebook and Twitter, but when I select Twitter it doesn't prefill the text as I would have expected.
I know that there are issues around doing this with Facebook, but I must be doing something wrong for it to not be working with Twitter.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Example Text");
startActivity(Intent.createChooser(intent, "Share Text"));
I'm using this snippet on my code:
private void shareTwitter(String message) {
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "This is a Test.");
tweetIntent.setType("text/plain");
PackageManager packManager = getPackageManager();
List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
boolean resolved = false;
for (ResolveInfo resolveInfo : resolvedInfoList) {
if (resolveInfo.activityInfo.packageName.startsWith("com.twitter.android")) {
tweetIntent.setClassName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name);
resolved = true;
break;
}
}
if (resolved) {
startActivity(tweetIntent);
} else {
Intent i = new Intent();
i.putExtra(Intent.EXTRA_TEXT, message);
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://twitter.com/intent/tweet?text=" + urlEncode(message)));
startActivity(i);
Toast.makeText(this, "Twitter app isn't found", Toast.LENGTH_LONG).show();
}
}
private String urlEncode(String s) {
try {
return URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.wtf(TAG, "UTF-8 should always be supported", e);
return "";
}
}
Hope it helps.
you can simply open the URL with the text and Twitter App will do it. ;)
String url = "http://www.twitter.com/intent/tweet?url=YOURURL&text=YOURTEXT";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
and it will also open the browser to login at the tweet if twitter app is not found.
Try this, I used it and worked great
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/intent/tweet?text=...."));
startActivity(browserIntent);
First you have to check if the twitter app installed on the device or not then share the text on twitter:
try
{
// Check if the Twitter app is installed on the phone.
getActivity().getPackageManager().getPackageInfo("com.twitter.android", 0);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setClassName("com.twitter.android", "com.twitter.android.composer.ComposerActivity");
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Your text");
startActivity(intent);
}
catch (Exception e)
{
Toast.makeText(getActivity(),"Twitter is not installed on this device",Toast.LENGTH_LONG).show();
}
For sharing text and image on Twitter, more controlled version of code is below, you can add more methods for sharing with WhatsApp, Facebook ... This is for official App and does not open browser if app not exists.
public class IntentShareHelper {
public static void shareOnTwitter(AppCompatActivity appCompatActivity, String textBody, Uri fileUri) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.setPackage("com.twitter.android");
intent.putExtra(Intent.EXTRA_TEXT,!TextUtils.isEmpty(textBody) ? textBody : "");
if (fileUri != null) {
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/*");
}
try {
appCompatActivity.startActivity(intent);
} catch (android.content.ActivityNotFoundException ex) {
ex.printStackTrace();
showWarningDialog(appCompatActivity, appCompatActivity.getString(R.string.error_activity_not_found));
}
}
public static void shareOnWhatsapp(AppCompatActivity appCompatActivity, String textBody, Uri fileUri){...}
private static void showWarningDialog(Context context, String message) {
new AlertDialog.Builder(context)
.setMessage(message)
.setNegativeButton(context.getString(R.string.close), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setCancelable(true)
.create().show();
}
}
For getting Uri from File, use below class:
public class UtilityFile {
public static #Nullable Uri getUriFromFile(Context context, #Nullable File file) {
if (file == null)
return null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
return FileProvider.getUriForFile(context, "com.my.package.fileprovider", file);
} catch (Exception e) {
e.printStackTrace();
return null;
}
} else {
return Uri.fromFile(file);
}
}
// Returns the URI path to the Bitmap displayed in specified ImageView
public static Uri getLocalBitmapUri(Context context, ImageView imageView) {
Drawable drawable = imageView.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable) {
bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
return null;
}
// Store image to default external storage directory
Uri bmpUri = null;
try {
// Use methods on Context to access package-specific directories on external storage.
// This way, you don't need to request external read/write permission.
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = getUriFromFile(context, file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
}
For writing FileProvider, use this link: https://github.com/codepath/android_guides/wiki/Sharing-Content-with-Intents

Open Facebook page from Android app?

from my Android app, I would like to open a link to a Facebook profile in the official Facebook app (if the app is installed, of course). For iPhone, there exists the fb:// URL scheme, but trying the same thing on my Android device throws an ActivityNotFoundException.
Is there a chance to open a Facebook profile in the official Facebook app from code?
This works on the latest version:
Go to https://graph.facebook.com/<user_name_here> (https://graph.facebook.com/fsintents for instance)
Copy your id
Use this method:
public static Intent getOpenFacebookIntent(Context context) {
try {
context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/<id_here>"));
} catch (Exception e) {
return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/<user_name_here>"));
}
}
This will open the Facebook app if the user has it installed. Otherwise, it will open Facebook in the browser.
EDIT: since version 11.0.0.11.23 (3002850) Facebook App do not support this way anymore, there's another way, check the response below from Jared Rummler.
In Facebook version 11.0.0.11.23 (3002850) fb://profile/ and fb://page/ no longer work. I decompiled the Facebook app and found that you can use fb://facewebmodal/f?href=[YOUR_FACEBOOK_PAGE]. Here is the method I have been using in production:
/**
* <p>Intent to open the official Facebook app. If the Facebook app is not installed then the
* default web browser will be used.</p>
*
* <p>Example usage:</p>
*
* {#code newFacebookIntent(ctx.getPackageManager(), "https://www.facebook.com/JRummyApps");}
*
* #param pm
* The {#link PackageManager}. You can find this class through {#link
* Context#getPackageManager()}.
* #param url
* The full URL to the Facebook page or profile.
* #return An intent that will open the Facebook page/profile.
*/
public static Intent newFacebookIntent(PackageManager pm, String url) {
Uri uri = Uri.parse(url);
try {
ApplicationInfo applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
if (applicationInfo.enabled) {
// http://stackoverflow.com/a/24547437/1048340
uri = Uri.parse("fb://facewebmodal/f?href=" + url);
}
} catch (PackageManager.NameNotFoundException ignored) {
}
return new Intent(Intent.ACTION_VIEW, uri);
}
Is this not easier?
For example within an onClickListener?
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/426253597411506"));
startActivity(intent);
} catch(Exception e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/appetizerandroid")));
}
PS. Get your id (the large number) from http://graph.facebook.com/[userName]
For Facebook page:
try {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/" + pageId));
} catch (Exception e) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/" + pageId));
}
For Facebook profile:
try {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/" + profileId));
} catch (Exception e) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/" + profileId));
}
...because none of the answers points out the difference
Both tested with Facebook v.27.0.0.24.15 and Android 5.0.1 on Nexus 4
Here's the way to do it in 2016. It works great, and is very simple to use.
I discovered this after looking into how emails sent by Facebook opened the Facebook app.
// e.g. if your URL is https://www.facebook.com/EXAMPLE_PAGE, you should
// put EXAMPLE_PAGE at the end of this URL, after the ?
String YourPageURL = "https://www.facebook.com/n/?YOUR_PAGE_NAME";
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(YourPageURL));
startActivity(browserIntent);
this is the simplest code for doing this
public final void launchFacebook() {
final String urlFb = "fb://page/"+yourpageid;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(urlFb));
// If a Facebook app is installed, use it. Otherwise, launch
// a browser
final PackageManager packageManager = getPackageManager();
List<ResolveInfo> list =
packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() == 0) {
final String urlBrowser = "https://www.facebook.com/"+pageid;
intent.setData(Uri.parse(urlBrowser));
}
startActivity(intent);
}
A more reusable approach.
This is a functionality we generally use in most of our apps. Hence here is a reusable piece of code to achieve this.
(Similar to other answers in terms for facts. Posting it here just to simplify and make the implementation reusable)
"fb://page/ does not work with newer versions of the FB app. You should use fb://facewebmodal/f?href= for newer versions. (Like mentioned in another answer here)
This is a full fledged working code currently live in one of my apps:
public static String FACEBOOK_URL = "https://www.facebook.com/YourPageName";
public static String FACEBOOK_PAGE_ID = "YourPageName";
//method to get the right URL to use in the intent
public String getFacebookPageURL(Context context) {
PackageManager packageManager = context.getPackageManager();
try {
int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
if (versionCode >= 3002850) { //newer versions of fb app
return "fb://facewebmodal/f?href=" + FACEBOOK_URL;
} else { //older versions of fb app
return "fb://page/" + FACEBOOK_PAGE_ID;
}
} catch (PackageManager.NameNotFoundException e) {
return FACEBOOK_URL; //normal web url
}
}
This method will return the correct url for app if installed or web url if app is not installed.
Then start an intent as follows:
Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = getFacebookPageURL(this);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);
That's all you need.
To do this we need the "Facebook page id", you can get it :
from the page go to "About".
go to "More Info" section.
To opening facebook app on specified profile page,
you can do this:
String facebookId = "fb://page/<Facebook Page ID>";
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId)));
Or you can validate when the facebook app is not installed, then open the facebook web page.
String facebookId = "fb://page/<Facebook Page ID>";
String urlPage = "http://www.facebook.com/mypage";
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId )));
} catch (Exception e) {
Log.e(TAG, "Application not intalled.");
//Open url web page.
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlPage)));
}
This has been reverse-engineered by Pierre87 on the FrAndroid forum, but I can't find anywhere official that describes it, so it's has to be treated as undocumented and liable to stop working at any moment:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.facebook.katana", "com.facebook.katana.ProfileTabHostActivity");
intent.putExtra("extra_user_id", "123456789l");
this.startActivity(intent);
try this code:
String facebookUrl = "https://www.facebook.com/<id_here>";
try {
int versionCode = getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
if (versionCode >= 3002850) {
Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
} else {
Uri uri = Uri.parse("fb://page/<id_here>");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
} catch (PackageManager.NameNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookUrl)));
}
As of March 2020 this works perfectly.
private void openFacebookPage(String pageId) {
String pageUrl = "https://www.facebook.com/" + pageId;
try {
ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo("com.facebook.katana", 0);
if (applicationInfo.enabled) {
int versionCode = getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
String url;
if (versionCode >= 3002850) {
url = "fb://facewebmodal/f?href=" + pageUrl;
} else {
url = "fb://page/" + pageId;
}
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
} else {
throw new Exception("Facebook is disabled");
}
} catch (Exception e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(pageUrl)));
}
}
After much testing I have found one of the most effective solutions:
private void openFacebookApp() {
String facebookUrl = "www.facebook.com/XXXXXXXXXX";
String facebookID = "XXXXXXXXX";
try {
int versionCode = getActivity().getApplicationContext().getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
if(!facebookID.isEmpty()) {
// open the Facebook app using facebookID (fb://profile/facebookID or fb://page/facebookID)
Uri uri = Uri.parse("fb://page/" + facebookID);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
} else if (versionCode >= 3002850 && !facebookUrl.isEmpty()) {
// open Facebook app using facebook url
Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
} else {
// Facebook is not installed. Open the browser
Uri uri = Uri.parse(facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
} catch (PackageManager.NameNotFoundException e) {
// Facebook is not installed. Open the browser
Uri uri = Uri.parse(facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
}
My answer builds on top of the widely-accepted answer from joaomgcd.
If the user has Facebook 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:
context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/620681997952698"));
You can use the following to decide what to do:
PackageInfo info = context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
if(info.applicationInfo.enabled)
return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/620681997952698"));
else
return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/620681997952698"));
Best answer I have found, it's working great.
Just go to your page on Facebook in the browser, right click, and click on "View source code", then find the page_id attribute: you have to use page_id here in this line after the last back-slash:
fb://page/pageID
For example:
Intent facebookAppIntent;
try {
facebookAppIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/1883727135173361"));
startActivity(facebookAppIntent);
} catch (ActivityNotFoundException e) {
facebookAppIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://facebook.com/CryOut-RadioTv-1883727135173361"));
startActivity(facebookAppIntent);
}
You can check it for all Facebook Apps that any of Facebook apps are installed or not .
For supporting OS level 11 we need to add this in AndrodiManifest.xml to avoid package name not found exception -
<manifest ...
<queries>
<package android:name="com.facebook.katana" />
<package android:name="com.facebook.lite" />
<package android:name="com.facebook.android" />
<package android:name="com.example.facebook" />
</queries>
<application .....
Then add this method to you code -
public static String isFacebookAppInstalled(Context context){
if(context!=null) {
PackageManager pm=context.getPackageManager();
ApplicationInfo applicationInfo;
//First check that if the main app of facebook is installed or not
try {
applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
return applicationInfo.enabled?"com.facebook.katana":"";
} catch (Exception ignored) {
}
//Then check that if the facebook lite is installed or not
try {
applicationInfo = pm.getApplicationInfo("com.facebook.lite", 0);
return applicationInfo.enabled?"com.facebook.lite":"";
} catch (Exception ignored) {
}
//Then check the other facebook app using different package name is installed or not
try {
applicationInfo = pm.getApplicationInfo("com.facebook.android", 0);
return applicationInfo.enabled?"com.facebook.android":"";
} catch (Exception ignored) {
}
try {
applicationInfo = pm.getApplicationInfo("com.example.facebook", 0);
return applicationInfo.enabled?"com.example.facebook":"";
} catch (Exception ignored) {
}
}
return "";
}
And then launch the app -
if (!TextUtils.isEmpty(isFacebookAppInstalled(context))) {
/* Facebook App is installed,So launch it.
It will return you installed facebook app's package
name which will be useful to launch the app */
Uri uri = Uri.parse("fb://facewebmodal/f?href=" + yourURL);
Intent intent = context.getPackageManager().getLaunchIntentForPackage(isFacebookAppInstalled(context);
if (intent != null) {
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
else {
Intent intentForOtherApp = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intentForOtherApp);
}
}
Intent intent = null;
try {
getPackageManager().getPackageInfo("com.facebook.katana", 0);
String url = "https://www.facebook.com/"+idFacebook;
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://facewebmodal/f?href="+url));
} catch (Exception e) {
// no Facebook app, revert to browser
String url = "https://facebook.com/"+idFacebook;
intent = new Intent(Intent.ACTION_VIEW);
intent .setData(Uri.parse(url));
}
this.startActivity(intent);
To launch facebook page from your app, let urlString = "fb://page/your_fb_page_id"
To launch facebook messenger let urlString = "fb-messenger://user/your_fb_page_id"
FB page id is usually numeric. To get it, goto Find My FB ID input your profile url, something like www.facebook.com/edgedevstudio then click "Find Numberic ID".
Voila, you now have your fb numeric id. replace "your_fb_page_id" with the generated Numeric ID
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(urlString))
if (intent.resolveActivity(packageManager) != null) //check if app is available to handle the implicit intent
startActivity(intent)
As of July 2018 this works perfectly with or without the Facebook app on all the devices.
private void goToFacebook() {
try {
String facebookUrl = getFacebookPageURL();
Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
private String getFacebookPageURL() {
String FACEBOOK_URL = "https://www.facebook.com/Yourpage-1548219792xxxxxx/";
String facebookurl = null;
try {
PackageManager packageManager = getPackageManager();
if (packageManager != null) {
Intent activated = packageManager.getLaunchIntentForPackage("com.facebook.katana");
if (activated != null) {
int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
if (versionCode >= 3002850) {
facebookurl = "fb://page/1548219792xxxxxx";
}
} else {
facebookurl = FACEBOOK_URL;
}
} else {
facebookurl = FACEBOOK_URL;
}
} catch (Exception e) {
facebookurl = FACEBOOK_URL;
}
return facebookurl;
}
Check out my code for opening Facebook specific Page:
//ID initialization
ImageView facebook = findViewById(R.id.facebookID);
facebook.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String facebookId = "fb://page/327031464582675";
String urlPage = "http://www.facebook.com/MDSaziburRahmanBD";
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId)));
}catch (Exception e){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlPage)));
}
}
});
I have created a method to open facebook page into facebook app, if app is not existing then opening in chrome
String socailLink="https://www.facebook.com/kfc";
Intent intent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = Utils.getFacebookUrl(getActivity(), socailLink);
if (facebookUrl == null || facebookUrl.length() == 0) {
Log.d("facebook Url", " is coming as " + facebookUrl);
return;
}
intent.setData(Uri.parse(facebookUrl));
startActivity(intent);
Utils.class add these method
public static String getFacebookUrl(FragmentActivity activity, String facebook_url) {
if (activity == null || activity.isFinishing()) return null;
PackageManager packageManager = activity.getPackageManager();
try {
int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
if (versionCode >= 3002850) { //newer versions of fb app
Log.d("facebook api", "new");
return "fb://facewebmodal/f?href=" + facebook_url;
} else { //older versions of fb app
Log.d("facebook api", "old");
return "fb://page/" + splitUrl(activity, facebook_url);
}
} catch (PackageManager.NameNotFoundException e) {
Log.d("facebook api", "exception");
return facebook_url; //normal web url
}
}
and this
/***
* this method used to get the facebook profile name only , this method split domain into two part index 0 contains https://www.facebook.com and index 1 contains after / part
* #param context contain context
* #param url contains facebook url like https://www.facebook.com/kfc
* #return if it successfully split then return "kfc"
*
* if exception in splitting then return "https://www.facebook.com/kfc"
*
*/
public static String splitUrl(Context context, String url) {
if (context == null) return null;
Log.d("Split string: ", url + " ");
try {
String splittedUrl[] = url.split(".com/");
Log.d("Split string: ", splittedUrl[1] + " ");
return splittedUrl.length == 2 ? splittedUrl[1] : url;
} catch (Exception ex) {
return url;
}
}
open fb on button click event without using facebook sdk
Intent FBIntent = new Intent(Intent.ACTION_SEND);
FBIntent.setType("text/plain");
FBIntent.setPackage("com.facebook.katana");
FBIntent.putExtra(Intent.EXTRA_TEXT, "The text you wanted to share");
try {
context.startActivity(FBIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(context, "Facebook have not been installed.", Toast.LENGTH_SHORT).show( );
}
1-to get your id go to your image profile and click right click and take copy link adress.
try {
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("fb://profile/id"));
startActivity(intent);
} catch(Exception e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("facebook url")));
}
}
});
Declare constants
private String FACEBOOK_URL="https://www.facebook.com/approids";
private String FACEBOOK_PAGE_ID="approids";
Declare Method
public String getFacebookPageURL(Context context) {
PackageManager packageManager = context.getPackageManager();
try {
int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
boolean activated = packageManager.getApplicationInfo("com.facebook.katana", 0).enabled;
if(activated){
if ((versionCode >= 3002850)) {
Log.d("main","fb first url");
return "fb://facewebmodal/f?href=" + FACEBOOK_URL;
} else {
return "fb://page/" + FACEBOOK_PAGE_ID;
}
}else{
return FACEBOOK_URL;
}
} catch (PackageManager.NameNotFoundException e) {
return FACEBOOK_URL;
}
}
Call Function
Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = getFacebookPageURL(MainActivity.this);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);
To open the Facebook page in Facebook app from your Webview add this code in your shouldOverrideUrlLoading method
if (url.startsWith("https://www.facebook.com || Your Facebook page address")) {
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("fb://facewebmodal/f?href=" + url)));
} catch (Exception e2) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
return true;
}
Answering this in October 2018. The working code is the one using the pageID. I just tested it and it is functional.
public static void openUrl(Context ctx, String url){
Uri uri = Uri.parse(url);
if (url.contains(("facebook"))){
try {
ApplicationInfo applicationInfo = ctx.getPackageManager().getApplicationInfo("com.facebook.katana", 0);
if (applicationInfo.enabled) {
uri = Uri.parse("fb://page/<page_id>");
openURI(ctx, uri);
return;
}
} catch (PackageManager.NameNotFoundException ignored) {
openURI(ctx, uri);
return;
}
}
I implemented in this form in webview using fragment- inside oncreate:
webView.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView viewx, String urlx)
{
if(Uri.parse(urlx).getHost().endsWith("facebook.com")) {
{
goToFacebook();
}
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlx));
viewx.getContext().startActivity(intent);
return true;
}
});
and outside onCreateView :
private void goToFacebook() {
try {
String facebookUrl = getFacebookPageURL();
Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
//facebook url load
private String getFacebookPageURL() {
String FACEBOOK_URL = "https://www.facebook.com/pg/XXpagenameXX/";
String facebookurl = null;
try {
PackageManager packageManager = getActivity().getPackageManager();
if (packageManager != null) {
Intent activated = packageManager.getLaunchIntentForPackage("com.facebook.katana");
if (activated != null) {
int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
if (versionCode >= 3002850) {
facebookurl = "fb://page/XXXXXXpage_id";
}
} else {
facebookurl = FACEBOOK_URL;
}
} else {
facebookurl = FACEBOOK_URL;
}
} catch (Exception e) {
facebookurl = FACEBOOK_URL;
}
return facebookurl;
}
fun getOpenFacebookIntent(context: Context, url: String) {
return try {
context.packageManager.getPackageInfo("com.facebook.katana", 0)
context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/$url/")))
} catch (e: Exception) {
context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
}
}
Answer worked for me but one missing thing was to add query in manifest, here is my solution:
private void openFacebookApp() {
String facebookUrl = "www.facebook.com/XXXXXXXXXX";
String facebookID = "XXXXXXXXX";
try {
int versionCode = getActivity().getApplicationContext().getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
if(!facebookID.isEmpty()) {
// open the Facebook app using facebookID (fb://profile/facebookID or fb://page/facebookID)
Uri uri = Uri.parse("fb://page/" + facebookID);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
} else if (versionCode >= 3002850 && !facebookUrl.isEmpty()) {
// open Facebook app using facebook url
Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
} else {
// Facebook is not installed. Open the browser
Uri uri = Uri.parse(facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
} catch (PackageManager.NameNotFoundException e) {
// Facebook is not installed. Open the browser
Uri uri = Uri.parse(facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
}
and add that in Manifest outside the application tag
<queries>
<package android:name="com.google.android.gm" />
<package android:name="com.facebook.katana" />
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="https" />
</intent>
<intent>
<action android:name="android.intent.action.DIAL" />
<data android:scheme="tel" />
</intent>
<intent>
<action android:name="android.intent.action.SEND" />
<data android:mimeType="*/*" />
</intent>
</queries>
I have been trying all of these methods with no luck because the ID... to find the one that works for my profile and for any profile you must go to the profile you want to open without been logged in (sorry for my english), inspect the page (in chrome Ctrl+Shift+I or just F12), and look for the line that holds the attribute al:android:url, the profile id should be there...
enter image description here
... And then:
public static void OpenFacebookProfile(String profileID, String username)
{
boolean isFacebookInstalled =
null != PA.getPackageManager().getLaunchIntentForPackage("com.facebook.katana");
Intent intent = isFacebookInstalled ?
new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/" + profileID)) :
new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/" + username));
PA.startActivity(intent);
}
try {
String[] parts = url.split("//www.facebook.com/profile.php?id=");
getPackageManager().getPackageInfo("com.facebook.katana", 0);
startActivity(new Intent (Intent.ACTION_VIEW, Uri.parse(String.format("fb://page/%s", parts[1].trim()))));
} catch (Exception e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}

Categories

Resources