你的代碼更改爲以下
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");
}
});
並有以下方法。
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")));
}
}
檢查Facebook是否已安裝。
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");
學分Find out if app is installed
嗨,它工作得很好。但是,可以選擇讓用戶選擇網絡瀏覽器還是應用程序,而不是直接啓動應用程序? – beginnerK
這種不尋常的,但你可以檢查應用程序是否安裝了我必須回答的方法,並讓用戶選擇打開應用程序或瀏覽器。 –
好吧!謝謝 – beginnerK