Advertisement
splash5

A way to get PackageInfo when app start

Jan 8th, 2014
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1.  package com.splash5.spa;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.Method;
  5.  
  6. import android.app.Application;
  7. import android.content.pm.PackageInfo;
  8. import android.util.Log;
  9.  
  10. public class MyApplication extends Application
  11. {
  12.     private static PackageInfo getPackageInfo() throws Exception
  13.     {
  14.         PackageInfo pi = null;
  15.        
  16.         final Class<?> activity_thread_class = Class.forName("android.app.ActivityThread");
  17.         final Class<?> package_manager_interface = Class.forName("android.content.pm.IPackageManager");        
  18.         final Class<?> android_version_class = Class.forName("android.os.Build$VERSION");
  19.  
  20.         final Method current_package_name_method = activity_thread_class.getMethod("currentPackageName");
  21.         final Method get_package_manager_method = activity_thread_class.getMethod("getPackageManager");
  22.         final Field sdk_int_field = android_version_class.getField("SDK_INT");
  23.  
  24.         final Object package_manager = get_package_manager_method.invoke(activity_thread_class);
  25.         final Object packageName = current_package_name_method.invoke(activity_thread_class);
  26.         final int sdk_int = sdk_int_field.getInt(android_version_class);
  27.  
  28.         if (sdk_int < 16)
  29.         {
  30.             final Method get_package_info = package_manager_interface.getMethod("getPackageInfo", new Class<?>[]{String.class, int.class});
  31.             pi = (PackageInfo)get_package_info.invoke(package_manager, packageName, 0);
  32.         }
  33.         else
  34.         {
  35.             // 4.1 : android.os.UserId, 4.2+ : android.os.UserHandle
  36.             final Class<?> user_handle_class = Class.forName((sdk_int < 17 ? "android.os.UserId" : "android.os.UserHandle"));
  37.  
  38.             final Method get_package_info = package_manager_interface.getMethod("getPackageInfo", new Class<?>[]{String.class, int.class, int.class});
  39.             final Method my_user_id = user_handle_class.getMethod("myUserId");
  40.             final Object userId = my_user_id.invoke(user_handle_class);
  41.                
  42.             pi = (PackageInfo)get_package_info.invoke(package_manager, packageName, 0, userId);
  43.         }
  44.  
  45.         return pi;
  46.     }
  47.    
  48.     static
  49.     {
  50.         try
  51.         {
  52.             final PackageInfo pi = getPackageInfo();
  53.  
  54.             Log.d("Reward", "package name = " + pi.packageName);
  55.             Log.d("Reward", "version name = " + pi.versionName);           
  56.         }
  57.         catch (Exception e)
  58.         {
  59.             e.printStackTrace();
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement