Example usage for android.app Activity getPackageName

List of usage examples for android.app Activity getPackageName

Introduction

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

Prototype

@Override
    public String getPackageName() 

Source Link

Usage

From source file:org.totschnig.myexpenses.activity.CommonCommands.java

/**
 * @return version number (versionCode)/*from w  w w  .  j av  a  2  s .  com*/
 */
public static int getVersionNumber(Activity ctx) {
    int version = -1;
    try {
        PackageInfo pi = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0);
        version = pi.versionCode;
    } catch (Exception e) {
        Log.e("MyExpenses", "Package name not found", e);
    }
    return version;
}

From source file:org.totschnig.myexpenses.activity.CommonCommands.java

/**
 * @return version name//  ww w  .  j  a v a  2s.c o  m
 */
public static String getVersionName(Activity ctx) {
    String version = "";
    try {
        PackageInfo pi = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0);
        version = pi.versionName;
    } catch (Exception e) {
        Log.e("MyExpenses", "Package name not found", e);
    }
    return version;
}

From source file:Main.java

public static void startInstalledAppDetailsActivity(@Nullable final Activity context) {
    if (context == null) {
        return;/*from  ww w.j  a v a  2 s . c  o  m*/
    }
    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

/**
 * For use in sample code only. Checks if the sample was set up correctly,
 * including changing the package name to a non-Google package name and
 * replacing the placeholder IDs. Shows alert dialogs to notify about problems.
 * DO NOT call this method from a production app, it's meant only for samples!
 * @param resIds the resource IDs to check for placeholders
 * @return true if sample is set up correctly; false otherwise.
 *//*from  w  w w  . j a v  a 2 s .co m*/
public static boolean verifySampleSetup(Activity activity, int... resIds) {
    StringBuilder problems = new StringBuilder();
    boolean problemFound = false;
    problems.append("The following set up problems were found:\n\n");

    // Did the developer forget to change the package name?
    if (activity.getPackageName().startsWith("com.google.example.games")) {
        problemFound = true;
        problems.append("- Package name cannot be com.google.*. You need to change the "
                + "sample's package name to your own package.").append("\n");
    }

    for (int i : resIds) {
        if (activity.getString(i).toLowerCase().contains("replaceme")) {
            problemFound = true;
            problems.append(
                    "- You must replace all " + "placeholder IDs in the ids.xml file by your project's IDs.")
                    .append("\n");
            break;
        }
    }

    if (problemFound) {
        problems.append("\n\nThese problems may prevent the app from working properly.");
        showAlert(activity, problems.toString());
        return false;
    }

    return true;
}

From source file:org.totschnig.myexpenses.activity.CommonCommands.java

/**
 * retrieve information about the current version
 * @return concatenation of versionName, versionCode and buildTime
 * buildTime is automatically stored in property file during build process
 *///w  w w  .  j  a  v  a 2 s. c o m
public static String getVersionInfo(Activity ctx) {
    String version = "";
    String versionname = "";
    try {
        PackageInfo pi = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0);
        version = " (revision " + pi.versionCode + ") ";
        versionname = pi.versionName;
        //versiontime = ", " + R.string.installed + " " + sdf.format(new Date(pi.lastUpdateTime));
    } catch (Exception e) {
        Log.e(MyApplication.TAG, "Package info not found", e);
    }
    String buildDate = BuildConfig.BUILD_DATE;

    final String flavor = TextUtils.isEmpty(BuildConfig.FLAVOR) ? "" : " " + BuildConfig.FLAVOR;
    String installer = ctx.getPackageManager().getInstallerPackageName(ctx.getPackageName());
    installer = TextUtils.isEmpty(installer) ? "" : " " + installer;
    return versionname + version + buildDate + flavor + installer;
}

From source file:Main.java

/**
 * Play video file from res folder./*from  w w  w.j a v a  2s . com*/
 * Then call video.start();
 * @param activity - current Activity
 * @param videoViewId R.id.introVideo
 * @param videoResourceId R.raw.intro - res/raw/intro.mp4
 * @return VideoView
 */
public static VideoView playVideo(Activity activity, int videoViewId, int videoResourceId,
        MediaPlayer.OnCompletionListener listener) {
    activity.getWindow().setFormat(PixelFormat.TRANSLUCENT);
    VideoView view = (VideoView) activity.findViewById(videoViewId);
    view.setVideoURI(
            Uri.parse("android.resource://" + activity.getPackageName() + File.separator + videoResourceId));
    view.setKeepScreenOn(true);
    view.setMediaController(null);
    view.setOnCompletionListener(listener);
    view.requestFocus();
    return view;
}

From source file:Main.java

static String getCallingPackageName(Activity activity) {
    // getCallingPackage() was unstable until android-18, use this
    String packageName = activity.getCallingActivity().getPackageName();
    if (TextUtils.isEmpty(packageName)) {
        packageName = activity.getIntent().getPackage();
    }//  ww  w.j ava2s.  c  o  m
    if (TextUtils.isEmpty(packageName)) {
        Log.e(activity.getPackageName(),
                "Received blank Panic.ACTION_DISCONNECT Intent, it must be sent using startActivityForResult()!");
    }
    return packageName;
}

From source file:com.canking.sdcardhelper.PermissionUtils.java

/**
 * ??/*from   ww  w  .j ava  2  s  . c  o m*/
 *
 * @param cxt
 * @param req
 * @return
 */
@TargetApi(23)
public static <C extends Context> boolean checkSettingAlertPermission(Activity cxt, int req, C t) {
    Activity activity = (Activity) cxt;
    if (!Settings.canDrawOverlays(activity.getBaseContext())) {
        Log.i(TAG, "Setting not permission");

        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + activity.getPackageName()));
        activity.startActivityForResult(intent, req);
        return false;
    }

    return true;
}

From source file:Main.java

public static boolean checkNotificationReadPermission(Activity activity) {
    String notiStr = Settings.Secure.getString(activity.getContentResolver(), "enabled_notification_listeners");
    if (notiStr != null && !TextUtils.isEmpty(notiStr)) {
        final String[] names = notiStr.split(":");
        for (String name : names) {
            ComponentName cn = ComponentName.unflattenFromString(name);
            if (cn != null) {
                if (activity.getPackageName().equals(cn.getPackageName())) {
                    return true;
                }/*w  ww  .  jav a  2  s  .  c om*/
            }
        }
    }
    return false;
}

From source file:com.youshe.yangyi.common_app.util.PermissionUtils.java

/**
 * ??/* w w w  .  j  a va  2 s.  c o  m*/
 *
 * @param cxt
 * @param req
 * @return
 */
@TargetApi(23)
public static boolean checkSettingAlertPermission(Object cxt, int req) {
    if (cxt instanceof Activity) {
        Activity activity = (Activity) cxt;
        if (!Settings.canDrawOverlays(activity.getBaseContext())) {
            Log.i(TAG, "Setting not permission");

            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + activity.getPackageName()));
            activity.startActivityForResult(intent, req);
            return false;
        }
    } else if (cxt instanceof Fragment) {
        Fragment fragment = (Fragment) cxt;
        if (!Settings.canDrawOverlays(fragment.getActivity())) {
            Log.i(TAG, "Setting not permission");

            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + fragment.getActivity().getPackageName()));
            fragment.startActivityForResult(intent, req);
            return false;
        }
    } else {
        throw new RuntimeException("cxt is net a activity or fragment");
    }

    return true;
}