Example usage for android.content ComponentName ComponentName

List of usage examples for android.content ComponentName ComponentName

Introduction

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

Prototype

private ComponentName(String pkg, Parcel in) 

Source Link

Usage

From source file:Main.java

public static String getBroadCasetMetaData(Context context, Class<? extends BroadcastReceiver> receiverClazz,
        String key) {/*from  ww  w  . j  a  v a2  s .  com*/
    try {
        return context.getPackageManager().getReceiverInfo(new ComponentName(context, receiverClazz),
                PackageManager.GET_META_DATA).metaData.getString(key);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Fetch the versionCode from the AndroicManifest.xml
 * /*  w w w.  j  a v  a2s .co m*/
 * @param context
 * @return the versionCode
 */
public static int getVersionCode(Context context) {
    try {
        ComponentName comp = new ComponentName(context, context.getClass());
        PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(), 0);
        return pinfo.versionCode;
    } catch (android.content.pm.PackageManager.NameNotFoundException e) {
        return 0;
    }
}

From source file:Main.java

/**
 * Fetch the versionName from the AndroicManifest.xml
 * /*from w w w .  j a v a  2  s . c o  m*/
 * @param context
 * @return the versionName
 */
public static String getVersionName(Context context) {
    try {
        ComponentName comp = new ComponentName(context, context.getClass());
        PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(), 0);
        return pinfo.versionName;
    } catch (android.content.pm.PackageManager.NameNotFoundException e) {
        return null;
    }
}

From source file:Main.java

public static void setComponentEnabled(PackageManager packageManager, Context context, Class<?>[] classes,
        boolean enabled) {
    int length = classes.length;
    for (int i = 0; i < length; i++) {
        Class<?> clazz = classes[i];
        final ComponentName c = new ComponentName(context, clazz.getName());
        packageManager.setComponentEnabledSetting(c, enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
                : PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    }/*from   ww w  . j av  a  2  s. co m*/
}

From source file:Main.java

public static int[] getAppWidgetIds(final Context context, final AppWidgetManager appWidgetManager,
        final Class<? extends AppWidgetProvider> widgetProviderClass) {
    ComponentName thisAppWidget = new ComponentName(context.getPackageName(), widgetProviderClass.getName());
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
    return appWidgetIds;
}

From source file:Main.java

public static boolean openAppActivity(Context context, String packageName, String activityName) {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    ComponentName cn = new ComponentName(packageName, activityName);
    intent.setComponent(cn);/* w w  w. ja  v a  2s  .  c  o m*/
    try {
        context.startActivity(intent);
        return true;
    } catch (ActivityNotFoundException e) {
        return false;
    }
}

From source file:Main.java

public static boolean isAccessibilityServiceEnabled(Context context,
        Class<? extends AccessibilityService> accessibilityService) {
    ComponentName expectedComponentName = new ComponentName(context, accessibilityService);

    String enabledServicesSetting = Settings.Secure.getString(context.getContentResolver(),
            Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
    if (enabledServicesSetting == null)
        return false;

    TextUtils.SimpleStringSplitter colonSplitter = new TextUtils.SimpleStringSplitter(':');
    colonSplitter.setString(enabledServicesSetting);

    while (colonSplitter.hasNext()) {
        String componentNameString = colonSplitter.next();
        ComponentName enabledService = ComponentName.unflattenFromString(componentNameString);

        if (enabledService != null && enabledService.equals(expectedComponentName))
            return true;
    }/*  ww  w  .  j a  v  a 2 s .  c om*/

    return false;
}

From source file:Main.java

public static Intent getComponentIntent(String packageName, String className, Bundle bundle) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    if (bundle != null)
        intent.putExtras(bundle);/*from   www. ja  v a  2  s  .  co m*/
    ComponentName cn = new ComponentName(packageName, className);
    intent.setComponent(cn);
    return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}

From source file:Main.java

/**
 * Enable/Disable Broadcast Receiver//www.j  av a2  s.c o m
 *
 * @param context
 *     the context
 * @param brClass
 *     the br class
 * @param enabled
 *     the enabled
 */
public static void setStateOfReceiver(Context context, Class<?> brClass, boolean enabled) {
    ComponentName receiverName = new ComponentName(context, brClass.getName());
    PackageManager pm = context.getPackageManager();

    int newstate;
    if (enabled) {
        newstate = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
    } else {
        newstate = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
    }

    pm.setComponentEnabledSetting(receiverName, newstate, PackageManager.DONT_KILL_APP);
}

From source file:Main.java

public static void jumpToSystemDownloadApk(Context context, String url) {
    Intent intent = new Intent("android.intent.action.VIEW");
    Uri data = Uri.parse(Html.fromHtml(url).toString());
    intent.setData(data);/*from   www  .j  av  a  2 s .c  om*/
    intent.setPackage("com.google.android.browser");
    intent.addCategory("android.intent.category.BROWSABLE");
    intent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
    context.startActivity(intent);
}