Example usage for android.content Intent setPackage

List of usage examples for android.content Intent setPackage

Introduction

In this page you can find the example usage for android.content Intent setPackage.

Prototype

public @NonNull Intent setPackage(@Nullable String packageName) 

Source Link

Document

(Usually optional) Set an explicit application package name that limits the components this Intent will resolve to.

Usage

From source file:Main.java

public static void startApkActivity(final Context ctx, String packageName) {
    PackageManager pm = ctx.getPackageManager();
    PackageInfo pi;//w w w .  j a va 2  s.c  o  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();
    }
}

From source file:Main.java

public static void jumpToSystemDownloadApk(Context context, String url) {
    Intent intent = new Intent("android.intent.action.VIEW");
    Uri data = Uri.parse(Html.fromHtml(url).toString());
    intent.setData(data);//from  w  w w  .j  a  v a 2 s.  c  o  m
    intent.setPackage("com.google.android.browser");
    intent.addCategory("android.intent.category.BROWSABLE");
    intent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
    context.startActivity(intent);
}

From source file:Main.java

public static void closeNaviBroadcast_auto(Context context) {
    Intent intent = new Intent("android.intent.action.VIEW",
            android.net.Uri.parse("androidauto://mapOpera?sourceApplication=launcher&traffic=1"));
    intent.setPackage("com.autonavi.amapauto");
    context.sendBroadcast(intent);//from  www  .  j  a  v  a  2s  .c  o  m

}

From source file:Main.java

public static void startNavi3D_auto(Context context) {

    Intent intent = new Intent("android.intent.action.VIEW",
            android.net.Uri.parse("androidauto://mapOpera?sourceApplication=launcher&switchView=2"));
    intent.setPackage("com.autonavi.amapauto");
    context.sendBroadcast(intent);//from  w  ww. j a va  2 s  . co m

}

From source file:Main.java

/**
 * open another app/* w w  w  . j a v  a  2  s .  co m*/
 * @param context
 * @param packageName
 * @throws NameNotFoundException 
 */
public static void openApp(Context context, String packageName) throws NameNotFoundException {
    PackageInfo pi = context.getPackageManager().getPackageInfo(packageName, 0);

    Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
    resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    resolveIntent.setPackage(pi.packageName);

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

    Iterator<ResolveInfo> iterator = apps.iterator();
    while (iterator.hasNext()) {
        ResolveInfo ri = iterator.next();
        if (ri != null) {
            packageName = ri.activityInfo.packageName;
            String className = ri.activityInfo.name;
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            ComponentName cn = new ComponentName(packageName, className);
            intent.setComponent(cn);
            context.startActivity(intent);
        }
    }
}

From source file:Main.java

public static void startNaviNightMode_auto(Context context) {

    Intent intent = new Intent("android.intent.action.VIEW",
            android.net.Uri.parse("androidauto://mapOpera?sourceApplication=launcher&switchNightMode=1"));
    intent.setPackage("com.autonavi.amapauto");
    context.sendBroadcast(intent);//from   w  w w  .j av a  2s .  co m

}

From source file:Main.java

public static void startAppByPackageName(Context context, String packageName) {
    PackageInfo pi = null;/*from  www .j  av a  2 s. c o  m*/
    try {
        pi = context.getPackageManager().getPackageInfo(packageName, 0);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }

    Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
    resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    resolveIntent.setPackage(pi.packageName);

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

    ResolveInfo ri = apps.iterator().next();
    if (ri != null) {
        String packageName1 = ri.activityInfo.packageName;
        String className = ri.activityInfo.name;

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        ComponentName cn = new ComponentName(packageName1, className);

        intent.setComponent(cn);
        context.startActivity(intent);
    }

}

From source file:Main.java

/**
 * @param packageName//from   www.j  a  v  a  2 s  .  co  m
 * @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);

    }

}

From source file:Main.java

public static List<Intent> createTakePictureIntentList(@NonNull Context context, @NonNull Uri outputFileUri) {
    List<Intent> cameraIntents = new ArrayList<>();
    Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    PackageManager packageManager = context.getPackageManager();
    List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo res : listCam) {
        String packageName = res.activityInfo.packageName;
        Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        cameraIntents.add(intent);// w w  w .  j  ava  2  s .  c  o  m
    }

    return cameraIntents;
}

From source file:Main.java

/**
 * Returns a copy of {@param intent} with a class name set, if a class inside this app
 * has a corresponding intent filter.//  w  w w .  j  a  va  2 s  .c  o m
 */
private static Intent getIntentInAppIfExists(Context context, Intent intent) {
    try {
        final Intent intentCopy = new Intent(intent);
        // Force this intentCopy to open inside the current app.
        intentCopy.setPackage(context.getPackageName());
        final List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intentCopy,
                PackageManager.MATCH_DEFAULT_ONLY);
        if (list != null && list.size() != 0) {
            // Now that we know the intentCopy will work inside the current app, we
            // can return this intent non-null.
            if (list.get(0).activityInfo != null && !TextUtils.isEmpty(list.get(0).activityInfo.name)) {
                // Now that we know the class name, we may as well attach it to intentCopy
                // to prevent the package manager from needing to find it again inside
                // startActivity(). This is only needed for efficiency.
                intentCopy.setClassName(context.getPackageName(), list.get(0).activityInfo.name);
            }
            return intentCopy;
        }
        return null;
    } catch (Exception e) {
        // Don't let the package manager crash our app. If the package manager can't resolve the
        // intent here, then we can still call startActivity without calling setClass() first.
        return null;
    }
}