获取App使用记录
博客专区 > 小孩er 的博客 > 博客详情
获取App使用记录
小孩er 发表于1年前
获取App使用记录
  • 发表于 1年前
  • 阅读 39
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

使用UsageStatsManager获取app运行记录

UsageStatsManager出现在android 5.0 API21之后,5.0之前是没有这个类的

	// 获取app的UsageStats
	public static List<UsageStats> getUsageStatsList(Context context) {
		UsageStatsManager usm = (UsageStatsManager) context
				.getSystemService("usagestats");
		Calendar calendar = Calendar.getInstance();
		long endTime = calendar.getTimeInMillis();
		calendar.add(Calendar.YEAR, -1);
		long startTime = calendar.getTimeInMillis();
		List<UsageStats> usageStatsList = usm.queryUsageStats(
				UsageStatsManager.INTERVAL_DAILY, startTime, endTime);
		return usageStatsList;
	}

	// 读取UsageStats里数据
	public static void printUsageStats(List<UsageStats> usageStatsList)
			throws IllegalAccessException, IllegalArgumentException,
			NoSuchFieldException {
		for (UsageStats u : usageStatsList) {
			String pkgName = u.getPackageName();// 包名
			Long foregroundtime = u.getTotalTimeInForeground();// 运行时长:
			int launcherCount = u.getClass().getDeclaredField("mLaunchCount")
					.getInt(u);// 运行次数,不能直接访问,反射获取
		}
	}

通常情况下默认是拿不到数据的,需要给予权限,并打开相应开关,部分手机开关页是隐藏的,需要代码跳转。

//需要的权限,再manifest注册后,则可以在“查看使用情况权限”控制页控制本app
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />
//跳转的方法
public static void statrtUsageAccessActivity(Context context) {
	Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
	context.startActivity(intent);
}

/data/system/usagestats 

-rw------- system   system      12992 2016-07-23 08:31 usage-20160722
-rw------- system   system       6532 2016-07-24 01:11 usage-20160723
-rw------- system   system        976 2016-07-25 08:18 usage-20160724
-rw------- system   system       9848 2016-07-26 05:25 usage-20160725
-rw------- system   system       7308 2016-07-26 17:57 usage-20160726
-rw------- system   system      50802 2016-07-26 05:25 usage-history.xml

其中usage-history.xml如下

<usage-history>
    <pkg name="com.sogou.activity.src">
        <comp name="com.sogou.weixintopic.read.WeixinHeadlineReadFirstActivity" lrt="1468237211692" />
        <comp name="com.sogou.activity.src.wxapi.WXEntryActivity" lrt="1467711877007" />
        <comp name="com.sogou.activity.src.SplashActivity" lrt="1468501310396" />
        <comp name="com.sogou.search.result.SuggestionActivity" lrt="1468237220015" />
        <comp name="com.sogou.search.result.SogouSearchActivity" lrt="1468405345331" />
        <comp name="com.sogou.activity.src.NewUserAnimGuideActivity" lrt="1467689925626" />
        <comp name="com.sogou.weixintopic.read.PicNewsReadActivity" lrt="1467710313758" />
        <comp name="com.sogou.weixintopic.read.activity.CommentListActivity" lrt="1467710289689" />
        <comp name="com.sogou.search.entry.EntryActivity" lrt="1468501310836" />
    </pkg>
    <pkg name="com.xiaomi.account">
        <comp name="com.xiaomi.account.ui.AuthorizeActivity" lrt="1466477718040" />
        <comp name="com.xiaomi.account.ui.LoginActivity" lrt="1465135903157" />
        <comp name="com.xiaomi.account.ui.AccountSettingsActivity" lrt="1465199758416" />
        <comp name="com.xiaomi.account.ui.WelcomeActivity" lrt="1465135903193" />
        <comp name="com.xiaomi.account.ui.UserDetailInfoActivity" lrt="1465175067590" />
        <comp name="com.xiaomi.account.ui.SnsListActivity" lrt="1465135914454" />
    </pkg>
    <pkg name="com.android.updater">
        <comp name="com.android.updater.MainActivity" lrt="1465955978078" />
    </pkg>
...............
</usage-history>

-----------------------分割线---------------------------------

4.4.4之前 

 com.android.internal.app.IUsageStats

 android 4.4.4_r1 4.4.3_r1 4.4.2_r1 4.4.1_r1 4.4_r1 4.3.1_r1 4.3_r2.1 4.3_r1 

4.2.2_r14.2.1_r1.2 4.2_r1 4.1.2_r1 4.1.1_r1 、4.0.4_r2.1 4.0.4_r1.2 2.0_r1 ...

应该是可以通过这个类获取相关信息

需要系统级app才可以

参考链接:blog.csdn.net/tanwei4199/article/details/17527145

// 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();
		}
	}

 

--------------------------------------------------------------

获取权限除了跳转到页面,手动给予之外 还可以利用下面的方式进行代码操作。

android权限管理--AppOpsManager

    android自带的 ,4.3出现4.4

AppOpsManagerCompat

    v4包下类 用来适配低版本android环境

7.0之后推荐使用 RuntimePermission

共有 人打赏支持
小孩er
粉丝 0
博文 8
码字总数 2823
×
小孩er
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: