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:im.delight.android.baselib.Social.java

/**
 * Constructs an email Intent for the given message details and opens the application choooser for this Intent
 *
 * @param recipient the recipient's email address
 * @param subject the subject of the message
 * @param body the body text as a string
 * @param captionRes the string resource ID for the application chooser's window title
 * @param restrictToPackage an optional package name that the Intent may be restricted to (or null)
 * @param context the Context instance to start the Intent from
 * @throws Exception if there was an error trying to launch the email Intent
 *///from  ww  w  .  j a  va 2  s . co  m
public static void sendMail(final String recipient, final String subject, final String body,
        final int captionRes, final String restrictToPackage, final Context context) throws Exception {
    final String uriString = "mailto:" + Uri.encode(recipient) + "?subject=" + Uri.encode(subject) + "&body="
            + Uri.encode(body);
    final Uri uri = Uri.parse(uriString);
    final Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
    emailIntent.setData(uri);
    if (restrictToPackage != null && restrictToPackage.length() > 0) {
        emailIntent.setPackage(restrictToPackage);
        if (context != null) {
            // launch the target app directly
            context.startActivity(emailIntent);
        }
    } else {
        if (context != null) {
            // offer a selection of all applications that can handle the email Intent
            context.startActivity(Intent.createChooser(emailIntent, context.getString(captionRes)));
        }
    }
}

From source file:com.airbop.library.simple.CommonUtilities.java

static void onGCMMessage(Context context, Bundle bundle) {
    Intent intent = new Intent(GCM_MESSAGE_ACTION);
    intent.putExtras(bundle);//from ww w .ja  v a  2 s.  c om
    String packageName = context.getPackageName();
    intent.setPackage(packageName);

    //sendLocalBroadcast(context, intent);
    context.sendBroadcast(intent);
}

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

static void internalUnregister(Context context) {
    Log.v(TAG, "Unregistering app " + context.getPackageName());
    Intent intent = new Intent(GCMConstants.INTENT_TO_GCM_UNREGISTRATION);
    intent.setPackage(GSF_PACKAGE);
    intent.putExtra(GCMConstants.EXTRA_APPLICATION_PENDING_INTENT,
            PendingIntent.getBroadcast(context, 0, new Intent(), 0));
    context.startService(intent);/*w ww  .ja v a 2s  . c o m*/
}

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

public static synchronized void internalRegister(Context context, String... senderIds) {
    if (senderIds == null || senderIds.length == 0) {
        throw new IllegalArgumentException("No senderIds");
    }/*from w  w  w. j a va  2s.  co m*/
    StringBuilder builder = new StringBuilder(senderIds[0]);
    for (int i = 1; i < senderIds.length; i++) {
        builder.append(',').append(senderIds[i]);
    }
    String senders = builder.toString();
    Log.v(TAG, "Registering app " + context.getPackageName() + " of senders " + senders);
    Intent intent = new Intent(GCMConstants.INTENT_TO_GCM_REGISTRATION);
    intent.setPackage(GSF_PACKAGE);
    intent.putExtra(GCMConstants.EXTRA_APPLICATION_PENDING_INTENT,
            PendingIntent.getBroadcast(context, 0, new Intent(), 0));
    intent.putExtra(GCMConstants.EXTRA_SENDER, senders);
    context.startService(intent);
}

From source file:com.ruesga.rview.misc.ActivityHelper.java

@SuppressWarnings("Convert2streamapi")
public static void openUri(Context ctx, Uri uri, boolean excludeRview) {
    try {//  w ww .  ja v  a 2  s .co  m
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        intent.putExtra(Constants.EXTRA_FORCE_SINGLE_PANEL, true);
        intent.putExtra(Constants.EXTRA_SOURCE, ctx.getPackageName());

        if (excludeRview) {
            // Use a different url to find all the browsers activities
            Intent test = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.es"));
            PackageManager pm = ctx.getPackageManager();
            List<ResolveInfo> activities = pm.queryIntentActivities(test, PackageManager.MATCH_DEFAULT_ONLY);

            List<Intent> targetIntents = new ArrayList<>();
            for (ResolveInfo ri : activities) {
                if (!ri.activityInfo.packageName.equals(ctx.getPackageName())) {
                    Intent i = new Intent(Intent.ACTION_VIEW, uri);
                    i.setPackage(ri.activityInfo.packageName);
                    i.putExtra(Constants.EXTRA_SOURCE, ctx.getPackageName());
                    i.putExtra(Constants.EXTRA_FORCE_SINGLE_PANEL, true);
                    targetIntents.add(i);
                }
            }

            if (targetIntents.size() == 0) {
                throw new ActivityNotFoundException();
            } else if (targetIntents.size() == 1) {
                ctx.startActivity(targetIntents.get(0));
            } else {
                Intent chooserIntent = Intent.createChooser(intent, ctx.getString(R.string.action_open_with));
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                        targetIntents.toArray(new Parcelable[] {}));
                ctx.startActivity(chooserIntent);
            }
        } else {
            ctx.startActivity(intent);
        }

    } catch (ActivityNotFoundException ex) {
        // Fallback to default browser
        String msg = ctx.getString(R.string.exception_browser_not_found, uri.toString());
        Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
    }
}

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

/**
 * Creates a {@link PendingIntent} to be used for creating and canceling the undo timeout alarm.
 *//*from w  ww .  j  av  a  2  s .c  o m*/
private static PendingIntent createUndoTimeoutPendingIntent(final Context context,
        final NotificationAction notificationAction) {
    final Intent intent = new Intent(ACTION_UNDO_TIMEOUT);
    intent.setPackage(context.getPackageName());
    putNotificationActionExtra(intent, notificationAction);
    final int requestCode = notificationAction.mNotificationId;
    final PendingIntent pendingIntent = PendingIntent.getService(context, requestCode, intent, 0);
    return pendingIntent;
}

From source file:im.delight.android.commons.Social.java

/**
 * Displays an application chooser and composes the described email using the selected application
 *
 * @param recipientEmail the recipient's email address
 * @param subjectText the subject line of the message
 * @param bodyText the body text of the message
 * @param captionRes a string resource ID for the title of the application chooser's window
 * @param restrictToPackage an application's package name to restricted the selection to
 * @param context a context reference/*from  ww w . j a  v  a2  s.  c  o m*/
 * @throws Exception if there was an error trying to launch the email application
 */
public static void sendMail(final String recipientEmail, final String subjectText, final String bodyText,
        final int captionRes, final String restrictToPackage, final Context context) throws Exception {
    final String uriString = "mailto:" + Uri.encode(recipientEmail) + "?subject=" + Uri.encode(subjectText)
            + "&body=" + Uri.encode(bodyText);
    final Uri uri = Uri.parse(uriString);
    final Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
    emailIntent.setData(uri);

    if (restrictToPackage != null && restrictToPackage.length() > 0) {
        emailIntent.setPackage(restrictToPackage);
        if (context != null) {
            // launch the target app directly
            context.startActivity(emailIntent);
        }
    } else {
        if (context != null) {
            // offer a selection of all applications that can handle the email Intent
            context.startActivity(Intent.createChooser(emailIntent, context.getString(captionRes)));
        }
    }
}

From source file:org.wso2.app.catalog.utils.CommonUtils.java

/**
 * Call EMM system app in COPE mode.//from  www . jav  a  2  s . co  m
 * @param context - Application context.
 * @param operation - Operation code.
 * @param appUri - App package/APK URI when an app operation executed.
 * @param appName - Application name.
 */
public static void callAgentApp(Context context, String operation, String appUri, String appName) {
    Intent intent = new Intent(Constants.AGENT_APP_SERVICE_NAME);
    Intent explicitIntent = createExplicitFromImplicitIntent(context, intent);
    if (explicitIntent != null) {
        intent = explicitIntent;
    }

    intent.putExtra("operation", operation);
    intent.setPackage(Constants.PACKAGE_NAME);

    if (appUri != null) {
        intent.putExtra("appUri", appUri);
    }

    if (appName != null) {
        intent.putExtra("appName", appName);
    }
    context.startService(intent);
}

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

/**
 * Creates and displays an Undo notification for the specified {@link NotificationAction}.
 *///from  w w w  .j  ava  2  s .c  om
public static void createUndoNotification(final Context context, NotificationAction action) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentTitle(context.getString(action.getActionTextResId()

    ));
    builder.setSmallIcon(R.drawable.ic_notification);
    builder.setWhen(action.getWhen());

    // disable sound & vibration
    builder.setDefaults(0);

    final RemoteViews undoView = new RemoteViews(context.getPackageName(), R.layout.undo_notification);
    undoView.setTextViewText(R.id.description_text, context.getString(action.mActionTextResId));

    final String packageName = context.getPackageName();

    final Intent clickIntent = new Intent(ACTION_UNDO);
    clickIntent.setPackage(packageName);
    putNotificationActionExtra(clickIntent, action);
    final PendingIntent clickPendingIntent = PendingIntent.getService(context, action.getNotificationId(),
            clickIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    undoView.setOnClickPendingIntent(R.id.status_bar_latest_event_content, clickPendingIntent);
    builder.setContent(undoView);

    // When the notification is cleared, we perform the destructive action
    final Intent deleteIntent = new Intent(ACTION_DESTRUCT);
    deleteIntent.setPackage(packageName);
    putNotificationActionExtra(deleteIntent, action);
    final PendingIntent deletePendingIntent = PendingIntent.getService(context, action.getNotificationId(),
            deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    builder.setDeleteIntent(deletePendingIntent);

    final Notification notification = builder.build();

    final NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(action.getNotificationId(), notification);

    sUndoNotifications.put(action.getNotificationId(), action);
    sNotificationTimestamps.put(action.getNotificationId(), action.mWhen);
}

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

/**
 * Creates a {@link PendingIntent} for the specified notification action.
 *///from  w  w w.j a  v  a2  s.c  o m
public static PendingIntent getNotificationActionPendingIntent(Context context, NotificationAction action) {
    final Intent intent = new Intent(action.getActionType());
    intent.setData(action.getTaskUri());
    intent.setPackage(context.getPackageName());
    putNotificationActionExtra(intent, action);

    return PendingIntent.getService(context, action.getNotificationId(), intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
}