获取app运行次数运行时间等信息

主要通过PkgUsageStats这个类来获取相关信息,在eclipse下编译,可通过复制framework的class.jar加入到工程,从而直接引用这个类,在源码下也能直接引用;也可以在eclipse单纯的sdk下使用反射来调用。注意权限!

PkgUsageStats.java的路径:framework/base/core/java/com/android/internal/os/PkgUsageStats.java

// use reflect
	// have to root and push into system/app
	private void getPkgUsageStats()
    {
		try {
			Class<?> cServiceManager = Class
					.forName("android.os.ServiceManager");
			Method mGetService = cServiceManager.getMethod("getService",
					java.lang.String.class);
			Object oRemoteService = mGetService.invoke(null, "usagestats");

			// IUsageStats oIUsageStats =
			// IUsageStats.Stub.asInterface(oRemoteService)
			Class<?> cStub = Class
					.forName("com.android.internal.app.IUsageStats$Stub");
			Method mUsageStatsService = cStub.getMethod("asInterface",
					android.os.IBinder.class);
			Object oIUsageStats = mUsageStatsService.invoke(null,
					oRemoteService);

			// PkgUsageStats[] oPkgUsageStatsArray =
			// mUsageStatsService.getAllPkgUsageStats();
			Class<?> cIUsageStatus = Class
					.forName("com.android.internal.app.IUsageStats");
			Method mGetAllPkgUsageStats = cIUsageStatus.getMethod(
					"getAllPkgUsageStats", (Class[]) null);
			Object[] oPkgUsageStatsArray = (Object[]) mGetAllPkgUsageStats
					.invoke(oIUsageStats, (Object[]) null);
			System.out.println(oPkgUsageStatsArray);

			Class<?> cPkgUsageStats = Class
					.forName("com.android.internal.os.PkgUsageStats");

			StringBuffer sb = new StringBuffer();
			sb.append("nerver used : ");
			for (Object pkgUsageStats : oPkgUsageStatsArray) {
				// get pkgUsageStats.packageName, pkgUsageStats.launchCount,
				// pkgUsageStats.usageTime
				String packageName = (String) cPkgUsageStats.getDeclaredField(
						"packageName").get(pkgUsageStats);
				int launchCount = cPkgUsageStats
						.getDeclaredField("launchCount").getInt(pkgUsageStats);
				long usageTime = cPkgUsageStats.getDeclaredField("usageTime")
						.getLong(pkgUsageStats);
				if(launchCount > 0)
					Log.v("getPkgUsageStats",packageName + "  count: " + launchCount + "  time:  "
						+ usageTime);
				else{
					sb.append(packageName+" ");
				}
			}
			Log.v("getPkgUsageStats",sb.toString());
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

直接引用的代码:

private void getPkgUsageStats2()
    {
		IUsageStats statsService = (IUsageStats) IUsageStats.Stub.
				asInterface(ServiceManager.getService("usagestats"));
		
		PkgUsageStats[] pkgStats = null;
		try {
			pkgStats = statsService.getAllPkgUsageStats();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(pkgStats != null)
		{
			StringBuffer sb = new StringBuffer();
			sb.append("nerver used : ");
			for(PkgUsageStats usageStats : pkgStats)
			{
				String packageName = usageStats.packageName;
				int launchCount = usageStats.launchCount;
				long usageTime = usageStats.usageTime;
				if(launchCount > 0)
					Log.v("getPkgUsageStats",packageName + "  count: " + launchCount + "  time:  "
						+ usageTime);
				else{
					sb.append(packageName+" ");
				}
			}


			Log.v("getPkgUsageStats",sb.toString());
		}
    }
需要system权限,可以push进去,或者使用和手机的framework一样的签名。

另外,得到的数据感觉不是很多,但是开机后没有刷新掉,留待以后补充。

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页