android 获得一个应用程序的启动次数,运行时间等信息

转自:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=207309

 

通过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)&&(aUseTime                       result = -1;
                  else {
                      result = 0;
                  }

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

             return result;
         }

如何看自己的android的详细使用信息:
我们使用Andoroid手机时想看看自己的手机的使用情况,那么我们又如何去操作呢?也是必需得像程序这样要自己写一个程序才能查看吧,如果用户不是编程的,那得怎么办,呵,其实我们查看自己的手机使用详情是没有那么复杂的,今天androidkaifa.com就会大家说一下如何查询自己的手机的使用情况,下面是具体的查看方法,
其实查看方法非常简单,直接进入Android的工程模式即可,操作步骤如下:(笔者的android手机系统是4.0)
1、首先进入Android手机操作系统的拨号界面,直接输入“*#*#4636#*#*”(不加引号)即可以快速进入Android操作系统的工程测试模式。
2、在“测试”模式菜单中有手机信息“Phone information”、电池信息“Battery information”、WI-FI信息“WI-FI information”、使用状态“Usage statistics”四个选项。
3、我们点击选择第二项“Battery information”进入电池信息,然后就可以看到手机电池的详细信息了,其中包括电量等级、电池状态、温度、电池材质、电压等等信息。
4: 我们相应点击其实的选择就可以看到其实的相应的使用详细信息

你可能感兴趣的:(Android)