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

/**
 * Retrieve launcher activity name of the application from the context
 *
 * @param context The context of the application package.
 * @return launcher activity name of this application. From the
 *         "android:name" attribute.// w  w w. j  a v a 2s  .  c om
 */
private static String getLauncherClassName(Context context) {
    PackageManager packageManager = context.getPackageManager();

    Intent intent = new Intent(Intent.ACTION_MAIN);
    // To limit the components this Intent will resolve to, by setting an
    // explicit package name.
    intent.setPackage(context.getPackageName());
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    // All Application must have 1 Activity at least.
    // Launcher activity must be found!
    ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);

    // get a ResolveInfo containing ACTION_MAIN, CATEGORY_LAUNCHER
    // if there is no Activity which has filtered by CATEGORY_DEFAULT
    if (info == null) {
        info = packageManager.resolveActivity(intent, 0);
    }

    return info.activityInfo.name;
}

From source file:cz.maresmar.sfm.service.plugin.PluginUtils.java

/**
 * Starts plugin according to API level//w ww.  ja  va 2s .c o  m
 *
 * @param context Some valid context
 * @param intent  Intent of plugin to be started
 * @see JobIntentService#enqueueWork(Context, ComponentName, int, Intent)
 */
public static void startPlugin(@NonNull Context context, @NonNull Intent intent) {
    // Test if the plugin exists
    PackageManager manager = context.getPackageManager();
    if (manager.queryIntentServices(intent, 0).size() != 1) {
        Timber.e("Plugin %s not found", intent.getComponent());
        throw new IllegalArgumentException("Plugin not found " + intent.getComponent());
    }

    // Finds jobId for selected plugin
    String pluginName = ProviderContract.buildString(intent.getComponent());
    int jobId;
    if (!mPluginsIds.containsKey(pluginName)) {
        jobId = mPluginsIds.size();
        mPluginsIds.put(pluginName, jobId);
    } else {
        jobId = mPluginsIds.get(pluginName);
    }

    // Starts plugin according to API level
    if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
            || (context.getPackageName().equals(intent.getComponent().getPackageName()))) {
        JobIntentService.enqueueWork(context, intent.getComponent(), jobId, intent);
    } else {
        // On Android >= O with external plugin use BroadcastContract.BROADCAST_PLAN_RUN to
        // start plugin, because planning of external APK's service is not allowed

        Intent plan = new Intent();
        // Explicitly select a package to communicate with
        plan.setPackage(intent.getComponent().getPackageName());
        plan.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);

        plan.setAction(ActionContract.BROADCAST_PLAN_RUN);
        plan.putExtra(ActionContract.EXTRA_JOB_ID, jobId);
        plan.putExtra(ActionContract.EXTRA_INTENT_TO_DO, intent);
        context.sendBroadcast(plan);
    }
}

From source file:Main.java

public static Intent getImageFromGalleryCamera(Context context) {
    // Determine Uri of camera image to save.
    File root = new File(
            Environment.getExternalStorageDirectory() + File.separator + "dianta/camera" + File.separator);
    root.mkdirs();/*from w  w  w .j av  a  2 s.  c om*/
    String fname = "dianta-" + System.currentTimeMillis() + ".jpg";
    File sdImageMainDirectory = new File(root, fname);
    Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);

    // camera
    List<Intent> cameraIntents = new ArrayList<>();
    Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    PackageManager lPackageManager = context.getPackageManager();
    List<ResolveInfo> listCam = lPackageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo rInfo : listCam) {
        String packageName = rInfo.activityInfo.packageName;
        Intent lIntent = new Intent(captureIntent);
        lIntent.setComponent(new ComponentName(rInfo.activityInfo.packageName, rInfo.activityInfo.name));
        lIntent.setPackage(packageName);
        //save camera result to external storage
        lIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        cameraIntents.add(lIntent);
    }

    //ugly hacks for camera helper
    lastCameraImageSaved = outputFileUri;

    // gallery
    Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_PICK);

    Intent chooserIntent = Intent.createChooser(galleryIntent, "Pilih Sumber");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
            cameraIntents.toArray(new Parcelable[cameraIntents.size()]));

    return chooserIntent;
}

From source file:com.duy.ascii.sharedcode.ShareUtil.java

public static void shareMessenger(String text, Context context) {
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, text);
    sendIntent.setType("text/plain");
    sendIntent.setPackage("com.facebook.orca");
    try {/*from   www.j  av  a 2s  .  c  o  m*/
        context.startActivity(sendIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(context, "Please Install Facebook Messenger", Toast.LENGTH_LONG).show();
    }
}

From source file:Main.java

/**
 * Goes through all apps that handle VIEW intents and have a warmup service. Picks
 * the one chosen by the user if there is one, otherwise makes a best effort to return a
 * valid package name./*from w  ww  .  j a v a  2s.  c  o  m*/
 *
 * This is <strong>not</strong> threadsafe.
 *
 * @param context {@link Context} to use for accessing {@link PackageManager}.
 * @return The package name recommended to use for connecting to custom tabs related components.
 */
public static String getPackageNameToUse(Context context) {
    if (sPackageNameToUse != null)
        return sPackageNameToUse;

    PackageManager pm = context.getPackageManager();
    // Get default VIEW intent handler.
    Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
    ResolveInfo defaultViewHandlerInfo = pm.resolveActivity(activityIntent, 0);
    String defaultViewHandlerPackageName = null;
    if (defaultViewHandlerInfo != null) {
        defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName;
    }

    // Get all apps that can handle VIEW intents.
    List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
    List<String> packagesSupportingCustomTabs = new ArrayList<>();
    for (ResolveInfo info : resolvedActivityList) {
        Intent serviceIntent = new Intent();
        serviceIntent.setAction(ACTION_CUSTOM_TABS_CONNECTION);
        serviceIntent.setPackage(info.activityInfo.packageName);
        if (pm.resolveService(serviceIntent, 0) != null) {
            packagesSupportingCustomTabs.add(info.activityInfo.packageName);
        }
    }

    // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents
    // and service calls.
    if (packagesSupportingCustomTabs.isEmpty()) {
        sPackageNameToUse = null;
    } else if (packagesSupportingCustomTabs.size() == 1) {
        sPackageNameToUse = packagesSupportingCustomTabs.get(0);
    } else if (!TextUtils.isEmpty(defaultViewHandlerPackageName)
            && !hasSpecializedHandlerIntents(context, activityIntent)
            && packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName)) {
        sPackageNameToUse = defaultViewHandlerPackageName;
    } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) {
        sPackageNameToUse = STABLE_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) {
        sPackageNameToUse = BETA_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) {
        sPackageNameToUse = DEV_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(LOCAL_PACKAGE)) {
        sPackageNameToUse = LOCAL_PACKAGE;
    }
    return sPackageNameToUse;
}

From source file:Main.java

public static void getCameraFromSelfCenter(Activity activity, boolean openFrontCamera) {
    initData(activity);/*from w w  w.  j av a2  s . c  o m*/
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    Intent intent_camera = activity.getPackageManager().getLaunchIntentForPackage("com.android.camera");
    if (intent_camera != null) {
        intent.setPackage("com.android.camera");
    }
    if (openFrontCamera) {
        intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
    }
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mAvatarUri);
    activity.startActivityForResult(intent, REQUEST_TAKE_PICTURE_SELF_CENTER);
}

From source file:Main.java

public static void preferPackageForIntent(Context context, Intent intent, String packageName) {
    PackageManager pm = context.getPackageManager();
    for (ResolveInfo resolveInfo : pm.queryIntentActivities(intent, 0)) {
        if (resolveInfo.activityInfo.packageName.equals(packageName)) {
            intent.setPackage(packageName);
            break;
        }//  w w w . j  a  v  a 2s  .  co  m
    }
}

From source file:com.elkriefy.android.apps.chubbytabby.CustomTabsHelper.java

/**
 * Goes through all apps that handle VIEW intents and have a warmup service. Picks the one
 * chosen by the user if there is one, otherwise makes a best effort to return a valid package
 * name./*from w  w  w .  ja va2s  .co m*/
 *
 * This is <strong>not</strong> threadsafe.
 *
 * @param context {@link Context} to use for accessing {@link PackageManager}.
 * @return The package name recommended to use for connecting to custom tabs related components.
 */
public static String getPackageNameToUse(Context context) {
    if (sPackageNameToUse != null)
        return sPackageNameToUse;

    PackageManager pm = context.getPackageManager();
    // Get default VIEW intent handler.
    Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.packtpub.com"));
    ResolveInfo defaultViewHandlerInfo = pm.resolveActivity(activityIntent, 0);
    String defaultViewHandlerPackageName = null;
    if (defaultViewHandlerInfo != null) {
        defaultViewHandlerPackageName = defaultViewHandlerInfo.activityInfo.packageName;
    }

    // Get all apps that can handle VIEW intents.
    List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities(activityIntent, 0);
    List<String> packagesSupportingCustomTabs = new ArrayList<>();
    for (ResolveInfo info : resolvedActivityList) {
        Intent serviceIntent = new Intent();
        serviceIntent.setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION);
        serviceIntent.setPackage(info.activityInfo.packageName);
        if (pm.resolveService(serviceIntent, 0) != null) {
            packagesSupportingCustomTabs.add(info.activityInfo.packageName);
        }
    }

    // Now packagesSupportingCustomTabs contains all apps that can handle both VIEW intents
    // and service calls.
    if (packagesSupportingCustomTabs.isEmpty()) {
        sPackageNameToUse = null;
    } else if (packagesSupportingCustomTabs.size() == 1) {
        sPackageNameToUse = packagesSupportingCustomTabs.get(0);
    } else if (!android.text.TextUtils.isEmpty(defaultViewHandlerPackageName)
            && !hasSpecializedHandlerIntents(context, activityIntent)
            && packagesSupportingCustomTabs.contains(defaultViewHandlerPackageName)) {
        sPackageNameToUse = defaultViewHandlerPackageName;
    } else if (packagesSupportingCustomTabs.contains(STABLE_PACKAGE)) {
        sPackageNameToUse = STABLE_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(BETA_PACKAGE)) {
        sPackageNameToUse = BETA_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(DEV_PACKAGE)) {
        sPackageNameToUse = DEV_PACKAGE;
    } else if (packagesSupportingCustomTabs.contains(LOCAL_PACKAGE)) {
        sPackageNameToUse = LOCAL_PACKAGE;
    }
    return sPackageNameToUse;
}

From source file:com.granita.tasks.notification.NotificationActionIntentService.java

private static PendingIntent getDelayActionIntent(Context context, int notificationId, Uri taskUri, long due,
        boolean delay1h, String timezone, boolean allday) {
    String action = null;/*from  www. j  a  v a 2s.c  om*/
    if (delay1h) {
        action = ACTION_DELAY_1H;
    } else {
        action = ACTION_DELAY_1D;
    }
    final Intent intent = new Intent(action);
    intent.setPackage(context.getPackageName());
    intent.setData(taskUri);
    intent.putExtra(EXTRA_TASK_DUE, due);
    intent.putExtra(EXTRA_TIMEZONE, timezone);
    intent.putExtra(EXTRA_ALLDAY, allday);
    intent.putExtra(EXTRA_NOTIFICATION_ID, notificationId);
    final PendingIntent pendingIntent = PendingIntent.getService(context, REQUEST_CODE_DELAY, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    return pendingIntent;
}

From source file:fr.free.nrw.commons.Utils.java

public static void handleGeoCoordinates(Context context, String coords) {
    try {//from w ww .ja  va2  s  .  c om
        Uri gmmIntentUri = Uri.parse("google.streetview:cbll=" + coords);
        Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
        mapIntent.setPackage("com.google.android.apps.maps");
        context.startActivity(mapIntent);
    } catch (ActivityNotFoundException ex) {
        Toast toast = Toast.makeText(context, context.getString(R.string.map_application_missing),
                LENGTH_SHORT);
        toast.show();
    }
}