Example usage for android.app Activity getPackageManager

List of usage examples for android.app Activity getPackageManager

Introduction

In this page you can find the example usage for android.app Activity getPackageManager.

Prototype

@Override
    public PackageManager getPackageManager() 

Source Link

Usage

From source file:Main.java

/** Launch a SMS intent if the device is capable.
 *
 * @param activity The parent activity (for context)
 * @param number The number to sms (not the full URI)
 * @param text The sms body/* w  w w . j  a v  a 2 s.  co  m*/
 */
public static void launchSmsIntent(final Activity activity, String number, String text) {
    Log.i(LOG_TAG, "Launch SMS intent to " + number);
    // create sms intent
    Uri smsUri = Uri.parse("smsto:" + number);
    Intent smsIntent = new Intent(Intent.ACTION_SENDTO, smsUri);
    smsIntent.putExtra("sms_body", text);
    // make sure there is an activity which can handle the intent.
    PackageManager smspackageManager = activity.getPackageManager();
    List<ResolveInfo> smsresolveInfos = smspackageManager.queryIntentActivities(smsIntent, 0);
    if (smsresolveInfos.size() > 0) {
        activity.startActivity(smsIntent);
    }
}

From source file:Main.java

public static void mail(Activity activity, String email, String subject, String content) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");

    intent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);

    if (content != null) {
        intent.putExtra(Intent.EXTRA_TEXT, content);
    }/*from   w  w w .ja v a 2 s . c  o  m*/

    final PackageManager manager = activity.getPackageManager();
    final List<ResolveInfo> matches = manager.queryIntentActivities(intent, 0);

    for (ResolveInfo info : matches) {
        if (info.activityInfo.packageName.endsWith(".gm")
                || info.activityInfo.name.toLowerCase().contains("gmail")) {
            intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
        } else if (info.activityInfo.packageName.endsWith(".email")
                || info.activityInfo.name.toLowerCase().contains("email")) {
            intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
        }
    }

    activity.startActivity(intent);
}

From source file:com.repkap11.repcast.utils.Utils.java

public static void showUpdateDialogIfNecessary(Activity activity) {
    try {//from   w w  w.  j a v  a  2s .c  o  m
        SharedPreferences prefs = activity.getSharedPreferences("CHANGELOG", Context.MODE_PRIVATE);
        int currentVersionCode = activity.getPackageManager().getPackageInfo(activity.getPackageName(),
                0).versionCode;
        boolean hasShownPrevious = prefs.getBoolean("has-shown-prefs-" + (currentVersionCode - 1), false);
        boolean hasShownCurrent = prefs.getBoolean("has-shown-prefs-" + currentVersionCode, false);
        //Log.e(TAG, "hasShownPrevious:" + hasShownPrevious + " hasShownCurrent:" + hasShownCurrent);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("has-shown-prefs-" + currentVersionCode, true);
        editor.putBoolean("has-shown-prefs-" + (currentVersionCode - 1), true);
        if ((hasShownPrevious && !hasShownCurrent)) {
            AlertDialog d = new AlertDialog.Builder(activity)
                    .setTitle("Changelog: App Version " + currentVersionCode)
                    .setMessage(activity.getResources().getString(R.string.changelog_message))
                    .setCancelable(false).setNegativeButton("Close", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                        }
                    }).show();
            //DisplayMetrics metrics = activity.getResources().getDisplayMetrics();
            //int width = metrics.widthPixels;
            //int height = metrics.heightPixels;
            //Log.e(TAG, "Width:" + width + " Height:" + height);
            //d.getWindow().setLayout(width,height);
        }
        editor.apply();
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

}

From source file:ru.dublgis.androidlocation.GmsLocationProvider.java

static public int getGmsVersion(Activity activity) {
    int versionCode = 0;

    try {//from ww w. j a va2 s . c  o  m
        versionCode = activity.getPackageManager()
                .getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0).versionCode;
    } catch (Exception e) {
        Log.e(TAG, "getGmsVersion(): " + e);
    }

    return versionCode;
}

From source file:com.vinexs.tool.Utility.java

public static String getAppName(Activity activity) {
    try {//from   w  ww  .  ja v a 2 s.  c  om
        PackageManager packageMgr = activity.getPackageManager();
        ActivityInfo activityInfo = packageMgr.getActivityInfo(activity.getComponentName(), 0);
        return activityInfo.loadLabel(packageMgr).toString();
    } catch (Exception e) {
        return "";
    }
}

From source file:Main.java

/** Launch an email intent if the device is capable.
 *
 * @param activity The parent activity (for context)
 * @param addr The address to email (not the full URI)
 * @param text The email body/*from   w w  w. j  ava 2 s. co m*/
 */
public static void launchEmailIntent(final Activity activity, String addr, String text) {
    Log.i(LOG_TAG, "Launch email intent from " + activity.getLocalClassName());
    // create email intent
    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { addr });
    emailIntent.setType("text/plain");
    // make sure there is an activity which can handle the intent.
    PackageManager emailpackageManager = activity.getPackageManager();
    List<ResolveInfo> emailresolveInfos = emailpackageManager.queryIntentActivities(emailIntent, 0);
    if (emailresolveInfos.size() > 0) {
        activity.startActivity(emailIntent);
    }
}

From source file:com.timrae.rikaidroid.MainActivity.java

public static boolean isAedictPresent(Activity context) {
    final Intent intent = new Intent("sk.baka.aedict3.action.ACTION_SEARCH_JMDICT");
    return !context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
            .isEmpty();/* w  ww.  ja  v a 2  s.c o  m*/
}

From source file:com.facebook.unity.FB.java

/**
 * Provides the key hash to solve the openSSL issue with Amazon
 * @return key hash//from   w w  w. j a v a  2 s .  co  m
 */
@TargetApi(Build.VERSION_CODES.FROYO)
public static String getKeyHash() {
    try {
        // In some cases the unity activity may not exist. This can happen when we are
        // completing a login and unity activity was killed in the background. In this
        // situation it's not necessary to send back the keyhash since the app will overwrite
        // the value with the value they get during the init call and the unity activity
        // wil be created by the time init is called.
        Activity activity = getUnityActivity();
        if (activity == null) {
            return "";
        }

        PackageInfo info = activity.getPackageManager().getPackageInfo(activity.getPackageName(),
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            String keyHash = Base64.encodeToString(md.digest(), Base64.DEFAULT);
            Log.d(TAG, "KeyHash: " + keyHash);
            return keyHash;
        }
    } catch (NameNotFoundException e) {
    } catch (NoSuchAlgorithmException e) {
    }
    return "";
}

From source file:com.mobivery.greent.MVYBatteryManagerActivityFragment.java

public static boolean isApplicationBroughtToBackground(final Activity activity) {
    ActivityManager activityManager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(1);

    // Check the top Activity against the list of Activities contained in the Application's package.
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        try {//from   www  .j  av  a2 s .c  om
            PackageInfo pi = activity.getPackageManager().getPackageInfo(activity.getPackageName(),
                    PackageManager.GET_ACTIVITIES);
            for (ActivityInfo activityInfo : pi.activities) {
                if (topActivity.getClassName().equals(activityInfo.name))
                    return false;
            }
        } catch (PackageManager.NameNotFoundException e) {
            return false; // Never happens.
        }
    }
    return true;
}

From source file:codepath.watsiapp.utils.Util.java

private static void startShareIntentWithExplicitSocialActivity(Activity ctx, ShareableItem patient,
        Set<String> socialActivitiesName, String displayName) {
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, patient.getShareableUrl());

    try {//from w ww  . j  a va 2s.c  om
        final PackageManager pm = ctx.getPackageManager();
        final List activityList = pm.queryIntentActivities(shareIntent, 0);
        int len = activityList.size();
        for (int i = 0; i < len; i++) {
            final ResolveInfo app = (ResolveInfo) activityList.get(i);
            Log.d("#####################", app.activityInfo.name);
            if (socialActivitiesName.contains(app.activityInfo.name)) {
                final ActivityInfo activity = app.activityInfo;
                final ComponentName name = new ComponentName(activity.applicationInfo.packageName,
                        activity.name);
                shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Fund Treatment");
                shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                shareIntent.setComponent(name);
                ctx.startActivity(shareIntent);
                break;
            }
        }
    } catch (final ActivityNotFoundException e) {
        Toast.makeText(ctx, "Could not find " + displayName + " app", Toast.LENGTH_SHORT).show();
    }

}