Check and request to install Facebook App

Last exercise "Start facbook app by startActivity(intent)", without checking if Facebook App installed. To check if a app installed currently, we can call getPackageManager().getApplicationInfo() method; if not installed, NameNotFoundException will be thrown. To open Google Play with with a specified app, startActivity with action of Intent.ACTION_VIEW and url of the target package.

Check and request to install Facebook App


Modify btnStartFacebookOnClickListener from the last exercise:
 OnClickListener btnStartFacebookOnClickListener
= new OnClickListener(){

@Override
public void onClick(View v) {

String facebookPackageName = "com.facebook.katana";
String facebookClassName = "com.facebook.katana.LoginActivity";

try {
ApplicationInfo facebookAppInfo = getPackageManager()
.getApplicationInfo(facebookPackageName, 0);
Intent intent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName(facebookPackageName, facebookClassName);
startActivity(intent);
} catch (NameNotFoundException e) {
// Didn't installed
Toast.makeText(getApplicationContext(), "Facebook not found! INSTALL.", Toast.LENGTH_LONG).show();

//Start Market to downoad and install Facebook App
Uri uri = Uri.parse("market://details?id=" + facebookPackageName);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}

}};


Next:
- Launch Facebook app from a specified page, using intent with ACTION_VIEW.