Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.List;

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

import android.content.pm.ResolveInfo;

public class Main {
    /**
     * @param packageName
     * @param context
     */
    public static void openApp(String packageName, Context context) {

        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);

        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        resolveIntent.setPackage(packageName);

        List<ResolveInfo> apps = context.getPackageManager().queryIntentActivities(resolveIntent, 0);

        ResolveInfo ri = apps.iterator().next();

        if (ri != null) {

            String packageName_i = ri.activityInfo.packageName;

            String className_i = ri.activityInfo.name;

            Intent intent = new Intent(Intent.ACTION_MAIN);

            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            intent.addCategory(Intent.CATEGORY_LAUNCHER);

            ComponentName cn = new ComponentName(packageName_i, className_i);

            intent.setComponent(cn);

            context.startActivity(intent);

        }

    }
}