Currently when user click the button , they will directly launch the Facebook and Twitter app. However I want to let user able to choose use browser or application to view the profile. Another problem is I can't find Instagram Protocol to launch the app to view the profile, I only can view on webpage. Anyone Got Idea?
btnFb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String uri = "fb://page/80829078894";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
}
});
btnTwitter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String uri = "twitter://user?user_id=87114940";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
}
});
btnInstagram.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String uri = "http://instagram.com/plaza_semanggi";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
}
});
Change your code to the following
btnFb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openFacebookIntent(ActivityMain.this, "80829078894");
}
});
btnTwitter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openTwiterIntent(ActivityMain.this, "87114940");
}
});
btnInstagram.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openInstagramIntent(ActivityMain.this, "plaza_semanggi");
}
});
And had the following methods.
public static void openFacebookIntent(Context context, String facebookID) {
Intent intent;
try {
context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/facebookID"));
} catch (Exception e) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/facebookID"));
}
context.startActivity(intent);
}
public static void openTwiterIntent(Context context, String twitterID) {
Intent intent;
try {
context.getPackageManager().getPackageInfo("com.twitter.android", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=twitterID"));
} catch (Exception e) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/twitterID"));
}
context.startActivity(intent);
}
public static void openInstagramIntent(Context context, String instagramID) {
Uri uri = Uri.parse("http://instagram.com/_u/instagramID");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setPackage("com.instagram.android");
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
context.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://instagram.com/instagramID")));
}
}
Check if the facebook is installed.
private boolean isAppInstalled(String uri)
{
PackageManager pm = getPackageManager();
boolean app_installed = false;
try
{
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e)
{
app_installed = false;
}
return app_installed ;
}
boolean appInstalled = isAppInstalled("com.facebook.katana");
credits to Find out if app is installed
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.