怎么通过反射方法获取com.android.internal.os.PkgUsageStats

 我来答
育知同创教育
2016-07-10 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
  通过com.android.internal.os.PkgUsageStats这个类可以得到一个应用程序的启动次数,运行时间等信息,功能很强大。但是google并没有将这个类作为API接口提供给开发者,如果在android源码下开发,可以通过以下代码来使用这个类:

  import com.android.internal.app.IUsageStats;
  import com.android.internal.os.PkgUsageStats;

  //比较两个应用程序的启动次数和运行时间
  public final int compare(ApplicationInfo a, ApplicationInfo b) {
  ComponentName aName = a.intent.getComponent();
  ComponentName bName = b.intent.getComponent();
  int result = 0;
  //get usagestats service
  IUsageStats mUsageStatsService = IUsageStats.Stub
  .asInterface(ServiceManager.getService("usagestats"));
  try {
  //get PkgUsageStats
  PkgUsageStats aStats = mUsageStatsService
  .getPkgUsageStats(aName);
  PkgUsageStats bStats = mUsageStatsService
  .getPkgUsageStats(bName);
  if(aStats!=null && bStats!=null) {
  if ((aStats.launchCount > bStats.launchCount)
  || ((aStats.launchCount == bStats.launchCount) && (aStats.usageTime > bStats.usageTime)))
  result = -1;
  else if ((aStats.launchCount < bStats.launchCount)
  || ((aStats.launchCount == bStats.launchCount) && (aStats.usageTime < bStats.usageTime)))
  result = 1;
  else {
  result = 0;
  }
  }else if(aStats!=null && bStats ==null) {
  result = -1;
  } else if(aStats==null && bStats !=null) {
  result = 1;
  }
  } catch (RemoteException e) {
  Log.i("TAG", "get package usage stats fail");
  }

  return result;
  }

  那么如果想在sdk中使用这个 类要如果作呢--可以使用反射 的方法,代码如下:
  public final int compare(ApplicationInfo a, ApplicationInfo b) {
  
  ComponentName aName = a.intent.getComponent();
  ComponentName bName = b.intent.getComponent();
  int aLaunchCount,bLaunchCount;
  long aUseTime,bUseTime;
  int result = 0;
  
  try {
  
  //获得ServiceManager类
  Class<?> ServiceManager = Class
  .forName("android.os.ServiceManager");
  
  //获得ServiceManager的getService方法
  Method getService = ServiceManager.getMethod("getService", java.lang.String.class);
  
  //调用getService获取RemoteService
  Object oRemoteService = getService.invoke(null, "usagestats");
  
  //获得IUsageStats.Stub类
  Class<?> cStub = Class
  .forName("com.android.internal.app.IUsageStats$Stub");
  //获得asInterface方法
  Method asInterface = cStub.getMethod("asInterface", android.os.IBinder.class);
  //调用asInterface方法获取IUsageStats对象
  Object oIUsageStats = asInterface.invoke(null, oRemoteService);
  //获得getPkgUsageStats(ComponentName)方法
  Method getPkgUsageStats = oIUsageStats.getClass().getMethod("getPkgUsageStats", ComponentName.class);
  //调用getPkgUsageStats 获取PkgUsageStats对象
  Object aStats = getPkgUsageStats.invoke(oIUsageStats, aName);
  Object bStats = getPkgUsageStats.invoke(oIUsageStats, bName);
  
  //获得PkgUsageStats类
  Class<?> PkgUsageStats = Class.forName("com.android.internal.os.PkgUsageStats");
  
  aLaunchCount = PkgUsageStats.getDeclaredField("launchCount").getInt(aStats);
  bLaunchCount = PkgUsageStats.getDeclaredField("launchCount").getInt(bStats);
  aUseTime = PkgUsageStats.getDeclaredField("usageTime").getLong(aStats);
  bUseTime = PkgUsageStats.getDeclaredField("usageTime").getLong(bStats);
  
  if((aLaunchCount>bLaunchCount)||
  ((aLaunchCount == bLaunchCount)&&(aUseTime>bUseTime)))
  result = 1;
  else if((aLaunchCount<bLaunchCount)||((aLaunchCount ==
  bLaunchCount)&&(aUseTime<bUseTime)))
  result = -1;
  else {
  result = 0;
  }

  } catch (Exception e) {
  Log.e("###", e.toString(), e);
  }

  return result;
  }
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式