Android Activity Start startApkActivity(final Context ctx, String packageName)

Here you can find the source of startApkActivity(final Context ctx, String packageName)

Description

start Apk Activity

Declaration

public static void startApkActivity(final Context ctx,
            String packageName) 

Method Source Code

//package com.java2s;

import java.util.List;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;

public class Main {
    public static void startApkActivity(final Context ctx,
            String packageName) {
        PackageManager pm = ctx.getPackageManager();
        PackageInfo pi;//  ww  w  . j av  a 2 s .co m
        try {
            pi = pm.getPackageInfo(packageName, 0);
            Intent intent = new Intent(Intent.ACTION_MAIN, null);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.setPackage(pi.packageName);

            List<ResolveInfo> apps = pm.queryIntentActivities(intent, 0);

            ResolveInfo ri = apps.iterator().next();
            if (ri != null) {
                String className = ri.activityInfo.name;
                intent.setComponent(new ComponentName(packageName,
                        className));
                ctx.startActivity(intent);
            }
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. goToActivity(Context currentActivity, Class newClass)
  2. goToActivity(Context currentActivity, Class newClass, List extras)
  3. openActivity(Context packageContext, int flags, Map pairs, boolean closeCurrentContext, Class activity)
  4. restartActivity(final Activity activity)
  5. restartPC(final Activity activity)
  6. startActivity(Context context, Class activity)
  7. startNewActivity(Class target, final Activity activity)