Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.splash5.spa;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- import android.app.Application;
- import android.content.pm.PackageInfo;
- import android.util.Log;
- public class MyApplication extends Application
- {
- private static PackageInfo getPackageInfo() throws Exception
- {
- PackageInfo pi = null;
- final Class<?> activity_thread_class = Class.forName("android.app.ActivityThread");
- final Class<?> package_manager_interface = Class.forName("android.content.pm.IPackageManager");
- final Class<?> android_version_class = Class.forName("android.os.Build$VERSION");
- final Method current_package_name_method = activity_thread_class.getMethod("currentPackageName");
- final Method get_package_manager_method = activity_thread_class.getMethod("getPackageManager");
- final Field sdk_int_field = android_version_class.getField("SDK_INT");
- final Object package_manager = get_package_manager_method.invoke(activity_thread_class);
- final Object packageName = current_package_name_method.invoke(activity_thread_class);
- final int sdk_int = sdk_int_field.getInt(android_version_class);
- if (sdk_int < 16)
- {
- final Method get_package_info = package_manager_interface.getMethod("getPackageInfo", new Class<?>[]{String.class, int.class});
- pi = (PackageInfo)get_package_info.invoke(package_manager, packageName, 0);
- }
- else
- {
- // 4.1 : android.os.UserId, 4.2+ : android.os.UserHandle
- final Class<?> user_handle_class = Class.forName((sdk_int < 17 ? "android.os.UserId" : "android.os.UserHandle"));
- final Method get_package_info = package_manager_interface.getMethod("getPackageInfo", new Class<?>[]{String.class, int.class, int.class});
- final Method my_user_id = user_handle_class.getMethod("myUserId");
- final Object userId = my_user_id.invoke(user_handle_class);
- pi = (PackageInfo)get_package_info.invoke(package_manager, packageName, 0, userId);
- }
- return pi;
- }
- static
- {
- try
- {
- final PackageInfo pi = getPackageInfo();
- Log.d("Reward", "package name = " + pi.packageName);
- Log.d("Reward", "version name = " + pi.versionName);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement