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:com.mvc.imagepicker.ImagePicker.java

private static List<Intent> addIntentsToList(Context context, List<Intent> list, Intent intent) {
    Log.i(TAG, "Adding intents of type: " + intent.getAction());
    List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(intent, 0);
    for (ResolveInfo resolveInfo : resInfo) {
        String packageName = resolveInfo.activityInfo.packageName;
        Intent targetedIntent = new Intent(intent);
        targetedIntent.setPackage(packageName);
        list.add(targetedIntent);/*from   www  . j  ava2  s .  c  o m*/
        Log.i(TAG, "App package: " + packageName);
    }
    return list;
}

From source file:Main.java

/**
 * This allows the app to specify a {@code packageName} to handle the {@code intent}, if the
 * {@code packageName} is available on the device and can handle it. An example use is to open
 * a Google + stream directly using the Google + app.
 *///from w  w w .  jav  a2  s  . c o  m
public static void preferPackageForIntent(Context context, Intent intent, String packageName) {
    PackageManager pm = context.getPackageManager();
    if (pm != null) {
        for (ResolveInfo resolveInfo : pm.queryIntentActivities(intent, 0)) {
            if (resolveInfo.activityInfo.packageName.equals(packageName)) {
                intent.setPackage(packageName);
                break;
            }
        }
    }
}

From source file:net.tawacentral.roger.secrets.OnlineAgentManager.java

/**
 * Cancel the active request/*  w w w .  j  av a2 s . c o  m*/
 */
public static void cancel() {
    OnlineAgentManager.active = false;
    Intent secretsIntent = new Intent(SYNC_CANCEL);
    secretsIntent.setPackage(requestAgent.getClassId());
    responseActivity.sendBroadcast(secretsIntent, SECRETS_PERMISSION);
}

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

private static PendingIntent getCompleteActionIntent(Context context, int notificationId, Uri taskUri) {
    final Intent intent = new Intent(NotificationActionIntentService.ACTION_COMPLETE);

    intent.setPackage(context.getPackageName());
    intent.setData(taskUri);//  ww  w .ja  v a  2  s  .  c o m
    intent.putExtra(EXTRA_NOTIFICATION_ID, notificationId);
    final PendingIntent pendingIntent = PendingIntent.getService(context, REQUEST_CODE_COMPLETE, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    return pendingIntent;
}

From source file:net.tawacentral.roger.secrets.OnlineAgentManager.java

/**
 * Sends secrets to the specified OSA./* w w w. j  av  a  2 s .  co  m*/
 * 
 * Returns true if secrets are successfully sent, but makes no guarantees that
 * the secrets were received.
 * 
 * A one-time key is sent to the OSA and must be returned in the reply for it
 * to be considered valid.
 * 
 * @param agent
 * @param secrets
 * @param activity
 * @return true if secrets were sent
 */
public static boolean sendSecrets(OnlineSyncAgent agent, ArrayList<Secret> secrets,
        SecretsListActivity activity) {
    requestAgent = agent;
    responseActivity = activity;
    responseKey = generateResponseKey();
    try {
        Intent secretsIntent = new Intent(SYNC);
        secretsIntent.setPackage(agent.getClassId());
        secretsIntent.putExtra(INTENT_RESPONSEKEY, OnlineAgentManager.responseKey);
        String secretString = FileUtils.toJSONSecrets(secrets).toString();
        secretsIntent.putExtra(INTENT_SECRETS, secretString);

        activity.sendBroadcast(secretsIntent, SECRETS_PERMISSION);
        Log.d(LOG_TAG, "Secrets sent to OSA " + agent.getClassId());
        active = true;
        return true;
    } catch (Exception e) {
        Log.e(LOG_TAG, "Error sending secrets to OSA", e);
        // ignore the exception, false will be returned below
    }
    return false;
}

From source file:androidx.media.session.MediaButtonReceiver.java

private static ComponentName getServiceComponentByAction(Context context, String action) {
    PackageManager pm = context.getPackageManager();
    Intent queryIntent = new Intent(action);
    queryIntent.setPackage(context.getPackageName());
    List<ResolveInfo> resolveInfos = pm.queryIntentServices(queryIntent, 0 /* flags */);
    if (resolveInfos.size() == 1) {
        ResolveInfo resolveInfo = resolveInfos.get(0);
        return new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name);
    } else if (resolveInfos.isEmpty()) {
        return null;
    } else {//w  w  w.ja va  2  s.co m
        throw new IllegalStateException(
                "Expected 1 service that handles " + action + ", found " + resolveInfos.size());
    }
}

From source file:androidx.media.session.MediaButtonReceiver.java

/**
 * @hide/*  www.jav a2  s.co m*/
 */
@RestrictTo(LIBRARY)
public static ComponentName getMediaButtonReceiverComponent(Context context) {
    Intent queryIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    queryIntent.setPackage(context.getPackageName());
    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> resolveInfos = pm.queryBroadcastReceivers(queryIntent, 0);
    if (resolveInfos.size() == 1) {
        ResolveInfo resolveInfo = resolveInfos.get(0);
        return new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
    } else if (resolveInfos.size() > 1) {
        Log.w(TAG, "More than one BroadcastReceiver that handles " + Intent.ACTION_MEDIA_BUTTON
                + " was found, returning null.");
    }
    return null;
}

From source file:Main.java

protected static void launchFallbackSupportUri(Context context) {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(FALLBACK_SUPPORT_URL));
    // Let Chrome know that this intent is from Chrome, so that it does not close the app when
    // the user presses 'back' button.
    intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
    intent.putExtra(Browser.EXTRA_CREATE_NEW_TAB, true);
    intent.setPackage(context.getPackageName());
    context.startActivity(intent);/*from  w w  w .  j  a  v  a  2 s.c  o m*/
}

From source file:com.google.android.gcm.GCMRegistrar.java

private static void checkReceiver(Context context, Set<String> allowedReceivers, String action) {
    PackageManager pm = context.getPackageManager();
    String packageName = context.getPackageName();
    Intent intent = new Intent(action);
    intent.setPackage(packageName);
    List<ResolveInfo> receivers = pm.queryBroadcastReceivers(intent, PackageManager.GET_INTENT_FILTERS);
    if (receivers.isEmpty()) {
        throw new IllegalStateException("No receivers for action " + action);
    }//from  w  ww  .  j  a v  a2 s  .  com
    if (Log.isLoggable(TAG, Log.VERBOSE)) {
        Log.v(TAG, "Found " + receivers.size() + " receivers for action " + action);
    }
    // make sure receivers match
    for (ResolveInfo receiver : receivers) {
        String name = receiver.activityInfo.name;
        if (!allowedReceivers.contains(name)) {
            throw new IllegalStateException(
                    "Receiver " + name + " is not set with permission " + GCMConstants.PERMISSION_GCM_INTENTS);
        }
    }
}

From source file:com.adguard.android.commons.BrowserUtils.java

private static ComponentName getYandexBrowser(Context context, String action) {
    Intent mainIntent = new Intent();
    mainIntent.setAction(action);/*  www  . j ava2s  .co  m*/

    for (String packageName : yandexBrowserPackageList) {
        mainIntent.setPackage(packageName);

        List<ResolveInfo> installedPackages = context.getPackageManager().queryIntentActivities(mainIntent,
                PackageManager.MATCH_DEFAULT_ONLY);

        if (!installedPackages.isEmpty()) {
            ResolveInfo resolveInfo = installedPackages.get(0);

            return new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
        }
    }

    return null;
}