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:at.diamonddogs.util.Utils.java

/**
 * Brings up the MAIN/LAUNCHER activity and clears the top
 *
 * @param context a {@link Context}//from   w w  w. j av  a 2s .co  m
 */
public static void returnToHome(Context context) {
    PackageManager pm = context.getPackageManager();
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.setPackage(context.getPackageName());
    List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
    Intent homeIntent = new Intent("android.intent.action.MAIN");
    homeIntent.addCategory("android.intent.category.LAUNCHER");
    homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    homeIntent.setComponent(new ComponentName(context.getPackageName(), activities.get(0).activityInfo.name));
    context.startActivity(homeIntent);
}

From source file:io.hypertrack.sendeta.util.images.EasyImage.java

private static Intent createChooserIntent(Context context, String chooserTitle, boolean showGallery)
        throws IOException {
    Uri outputFileUri = createCameraPictureFile(context);
    List<Intent> cameraIntents = new ArrayList<>();
    Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    PackageManager packageManager = context.getPackageManager();
    List<ResolveInfo> camList = packageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo res : camList) {
        final String packageName = res.activityInfo.packageName;
        final 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);/*from ww  w  .  j  av a2 s .  co m*/
    }
    Intent galleryIntent;

    if (showGallery) {
        galleryIntent = createGalleryIntent();
    } else {
        galleryIntent = createDocumentsIntent();
    }

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

    return chooserIntent;
}

From source file:com.pdftron.pdf.utils.ViewerUtils.java

public static Uri openImageIntent(Fragment fragment, int requestCode) {
    // Determine Uri of camera image to save.
    final String fname = "IMG_" + System.currentTimeMillis() + ".jpg";
    File sdImageMainDirectory = new File(fragment.getActivity().getExternalCacheDir(), fname);
    Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);

    // Camera.//w  ww  .  j  av a  2s .  c om
    final List<Intent> cameraIntents = new ArrayList<>();
    final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    final PackageManager packageManager = fragment.getActivity().getPackageManager();
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo res : listCam) {
        final String packageName = res.activityInfo.packageName;
        final 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);
    }

    // Filesystem.
    final Intent galleryIntent = new Intent(Intent.ACTION_PICK);
    galleryIntent.setType("image/*");
    //galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

    // Chooser of filesystem options.
    final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");

    // Add the camera options.
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[] {}));

    fragment.startActivityForResult(chooserIntent, requestCode);
    return outputFileUri;
}

From source file:Main.java

public static void goToMiuiPermissionActivity_V8(Context context) {
    Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
    intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
    //        intent.setPackage("com.miui.securitycenter");
    intent.putExtra("extra_pkgname", context.getPackageName());
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    if (isIntentAvailable(intent, context)) {
        context.startActivity(intent);//from  w  w w . j  ava  2 s . c  o  m
    } else {
        intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
        intent.setPackage("com.miui.securitycenter");
        intent.putExtra("extra_pkgname", context.getPackageName());
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        if (isIntentAvailable(intent, context)) {
            context.startActivity(intent);
        } else {
            Log.e(TAG, "Intent is not available!");
        }
    }
}

From source file:org.dmfs.tasks.notification.NotificationActionUtils.java

/**
 * Creates a {@link PendingIntent} to be used for creating and canceling the undo timeout alarm.
 *//*w w w  .j  a v a 2s.c o m*/
private static PendingIntent createUndoTimeoutPendingIntent(final Context context,
        final NotificationAction notificationAction) {
    final Intent intent = new Intent(context, NotificationUpdaterService.class);
    intent.setAction(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:Main.java

public static void shareOnFacebook(Context pContext, String urlToShare) {

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    // intent.putExtra(Intent.EXTRA_SUBJECT, "Foo bar"); // NB: has no effect!
    intent.putExtra(Intent.EXTRA_TEXT, urlToShare);

    // See if official Facebook app is found
    boolean facebookAppFound = false;
    List<ResolveInfo> matches = pContext.getPackageManager().queryIntentActivities(intent, 0);
    for (ResolveInfo info : matches) {
        if (info.activityInfo.packageName.toLowerCase().startsWith("com.facebook.katana")) {
            intent.setPackage(info.activityInfo.packageName);
            facebookAppFound = true;//from   w  ww  .j  a  va2  s . co  m
            break;
        }
    }

    // As fallback, launch sharer.php in a browser
    if (!facebookAppFound) {
        String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + urlToShare;
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
    }

    pContext.startActivity(intent);
}

From source file:com.vk.sdk.payments.VKIInAppBillingService.java

/**
 * Method for save transaction if you can't use
 * VKIInAppBillingService mService = new VKIInAppBillingService(IInAppBillingService.Stub.asInterface(service));
 * WARNING!!! this method must call before consume google and is it returned true
 *
 * @param apiVersion - version google apis
 * @param purchaseToken - purchase token
 * @return true is send is ok/*from   w  w w . ja va 2 s . co m*/
 * @throws android.os.RemoteException
 */
public static boolean consumePurchaseToVk(final int apiVersion, @NonNull final String purchaseToken)
        throws android.os.RemoteException {
    if (Looper.getMainLooper().equals(Looper.myLooper())) {
        throw new RuntimeException("Network on main thread");
    }
    final Context ctx = VKUIHelper.getApplicationContext();
    if (ctx == null) {
        return false;
    }

    final PurchaseData purchaseData = new PurchaseData();

    if (!VKPaymentsServerSender.isNotVkUser()) {
        final SyncServiceConnection serviceConnection = new SyncServiceConnection() {
            @Override
            public void onServiceConnectedImpl(ComponentName name, IBinder service) {
                Object iInAppBillingService = null;

                final Class<?> iInAppBillingServiceClassStub;
                try {
                    iInAppBillingServiceClassStub = Class
                            .forName("com.android.vending.billing.IInAppBillingService$Stub");
                    Method asInterface = iInAppBillingServiceClassStub.getMethod("asInterface",
                            android.os.IBinder.class);
                    iInAppBillingService = asInterface.invoke(iInAppBillingServiceClassStub, service);
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException(PARAMS_ARE_NOT_VALID_ERROR);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }

                try {
                    purchaseData.purchaseData = getPurchaseData(iInAppBillingService, apiVersion,
                            ctx.getPackageName(), purchaseToken);
                } catch (Exception e) {
                    Log.e("VKSDK", "error", e);
                    purchaseData.hasError = true;
                }
            }

            @Override
            public void onServiceDisconnectedImpl(ComponentName name) {

            }
        };

        Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
        serviceIntent.setPackage("com.android.vending");
        if (!ctx.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {
            // bind
            ctx.bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
            // wait bind
            synchronized (serviceConnection.syncObj) {
                while (!serviceConnection.isFinish) {
                    try {
                        serviceConnection.syncObj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            // unbind
            ctx.unbindService(serviceConnection);
        } else {
            return false;
        }
    } else {
        return true;
    }

    if (purchaseData.hasError) {
        return false;
    } else if (!TextUtils.isEmpty(purchaseData.purchaseData)) {
        VKPaymentsServerSender.getInstance(ctx).saveTransaction(purchaseData.purchaseData);
    }

    return true;
}

From source file:Main.java

public static Intent getLaunchIntent(Context context, String title, String url, String sel) {
    Intent intent = null;
    String number = parseTelephoneNumber(sel);
    if (number != null) {
        intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + number));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    } else {/*  ww w. ja v a  2s  .c o  m*/
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (isMapsURL(url)) {
            intent.setClassName(GMM_PACKAGE_NAME, GMM_CLASS_NAME);
        } else if (isYouTubeURL(url)) {
            intent.setPackage(YT_PACKAGE_NAME);
        }

        // Fall back if we can't resolve intent (i.e. app missing)
        PackageManager pm = context.getPackageManager();
        if (null == intent.resolveActivity(pm)) {
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
    }

    if (sel != null && sel.length() > 0) {
        ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        cm.setText(sel);
    }

    return intent;
}

From source file:org.dmfs.tasks.notification.NotificationActionUtils.java

/**
 * Creates and displays an Undo notification for the specified {@link NotificationAction}.
 *///from   w w w.  j a  v a  2s . co m
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(context, NotificationUpdaterService.class);
    clickIntent.setAction(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(context, NotificationUpdaterService.class);
    deleteIntent.setAction(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.ruesga.rview.misc.ActivityHelper.java

public static void handleUri(Context ctx, Uri uri) {
    try {//  w  ww  .  j  a v a 2s. c  om
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        intent.putExtra(Constants.EXTRA_FORCE_SINGLE_PANEL, true);
        intent.putExtra(Constants.EXTRA_HAS_PARENT, true);
        intent.putExtra(Constants.EXTRA_SOURCE, ctx.getPackageName());
        intent.setPackage(ctx.getPackageName());
        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();
    }
}