Android How to - Start an App from Context by package name and class name








Question

We would like to know how to start an App from Context by package name and class name.

Answer

//from   w  w  w  .jav  a2  s  .  c om
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;

public class Main {
  public static boolean startApp(Context context, String packageName, String className){
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setClassName(packageName, className);
    
    try {
      context.startActivity(intent);
    } catch (ActivityNotFoundException e) {
      return false;
    }
    
    return true;
  }
}