Example usage for android.content Intent addCategory

List of usage examples for android.content Intent addCategory

Introduction

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

Prototype

public @NonNull Intent addCategory(String category) 

Source Link

Document

Add a new category to the intent.

Usage

From source file:Main.java

public static void startAppByPackageName(Context context, String packageName) {
    PackageInfo pi = null;//from  w ww .j a  va2  s.com
    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

public static void imitateHome(Context context) {
    try {/*  ww  w  .  j av  a  2 s .  co m*/
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_HOME);
        context.startActivity(intent);
    } catch (Exception e) {
        // TODO: handle exception
    }
}

From source file:Main.java

public static void openImageAction(Activity activity, int requestCode) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    activity.startActivityForResult(intent, requestCode);
}

From source file:Main.java

public static void createDeskShortCut(Context cxt, String shortCutName, int icon, Class<?> cls) {
    Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    shortcutIntent.putExtra("duplicate", false);
    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortCutName);
    Parcelable ico = Intent.ShortcutIconResource.fromContext(cxt.getApplicationContext(), icon);
    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, ico);
    Intent intent = new Intent(cxt, cls);
    intent.setAction("android.intent.action.MAIN");
    intent.addCategory("android.intent.category.LAUNCHER");
    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
    cxt.sendBroadcast(shortcutIntent);/*ww  w .  j av  a 2s .  c om*/
}

From source file:Main.java

/**
 * @param packageName//  w w w  .  j  a  v  a 2s .  c  o  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 boolean isAppInstalled(Context context, String desiredPackageName) {
    Intent intent = new Intent("android.intent.action.MAIN");
    intent.addCategory("android.intent.category.LAUNCHER");

    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    List<ResolveInfo> resolveInfoList = context.getPackageManager().queryIntentActivities(intent, 0);

    for (ResolveInfo info : resolveInfoList) {
        if (info.activityInfo.packageName.equalsIgnoreCase(desiredPackageName)) {
            return true;
        }/*  w ww.j a v a2  s.  c  om*/
    }
    return false;
}

From source file:Main.java

public static void delShortcutFromDesktop(Context paramContext, String packageName, String cls,
        String appName) {/*ww  w  .j  a  v  a 2s.c o  m*/
    Intent localIntent1 = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
    String str = appName;
    PackageManager localPackageManager = paramContext.getPackageManager();
    int i = 8320;
    try {
        ApplicationInfo localApplicationInfo = localPackageManager.getApplicationInfo(packageName, i);
        if (str == null)
            str = localPackageManager.getApplicationLabel(localApplicationInfo).toString();
        localIntent1.putExtra("android.intent.extra.shortcut.NAME", str);
        ComponentName localComponentName = new ComponentName(packageName, cls);
        Intent localIntent2 = new Intent(Intent.ACTION_MAIN).setComponent(localComponentName);
        localIntent2.addCategory(Intent.CATEGORY_LAUNCHER);
        localIntent1.putExtra("android.intent.extra.shortcut.INTENT", localIntent2);
        paramContext.sendBroadcast(localIntent1);
        return;
    } catch (PackageManager.NameNotFoundException localNameNotFoundException) {
        while (true)
            localNameNotFoundException.printStackTrace();
    }
}

From source file:Main.java

/**
 * Open an URL in a message./*from   w w  w.  ja v  a2  s  . c om*/
 *
 * This is intended to mirror the operation of the original
 * (see android.webkit.CallbackProxy) with one addition of intent flags
 * "FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET".  This improves behavior when sublaunching
 * other apps via embedded URI's.
 *
 * We also use this hook to catch "mailto:" links and handle them locally.
 *
 * @param activity parent activity
 * @param url URL to open
 * @param senderAccountId if the URL is mailto:, we use this account as the sender.
 *        TODO When MessageCompose implements the account selector, this won't be necessary.
 *        Pass {@link Account#NO_ACCOUNT} to use the default account.
 * @return true if the URI has successfully been opened.
 */
public static boolean openUrlInMessage(Activity activity, String url, long senderAccountId) {
    // hijack mailto: uri's and handle locally
    //***
    //if (url != null && url.toLowerCase().startsWith("mailto:")) {
    //    return MessageCompose.actionCompose(activity, url, senderAccountId);
    //}

    // Handle most uri's via intent launch
    boolean result = false;
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    intent.addCategory(Intent.CATEGORY_BROWSABLE);
    intent.putExtra(Browser.EXTRA_APPLICATION_ID, activity.getPackageName());
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    try {
        activity.startActivity(intent);
        result = true;
    } catch (ActivityNotFoundException ex) {
        // No applications can handle it.  Ignore.
    }
    return result;
}

From source file:Main.java

public static void startInstalledAppDetailsActivity(@Nullable final Activity context) {
    if (context == null) {
        return;// w w w.  ja va 2  s .c om
    }
    final Intent i = new Intent();
    i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    i.addCategory(Intent.CATEGORY_DEFAULT);
    i.setData(Uri.parse("package:" + context.getPackageName()));
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    context.startActivity(i);
}

From source file:Main.java

/**
 * <pre>/*from  ww  w .  ja  va 2  s  .com*/
 * Start other app by its package name.
 * </pre>
 * @param packageName app 's package name
 */
public static void openApp(String packageName) {
    Context context = getCurrentContext();
    PackageManager manager = context.getPackageManager();
    Intent intent = manager.getLaunchIntentForPackage(packageName);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    context.startActivity(intent);
}