Android How to - Start App with package name








Question

We would like to know how to start App with package name.

Answer

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
//from w  w w.  j  a  va 2s.  co  m
public class Main {
  public static boolean startApp(Context context, String packageName){
    try {
      Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
      if(intent != null){
            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            context.startActivity(intent);
      } else {
        return false;
      }
        } catch(ActivityNotFoundException e){
          return false;
        }
    
    return true;
  }
}