android 应用程序使用统计

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");  
  
        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);  
            System.out.println(packageName + ":" + launchCount + ":"  
                    + usageTime);  


这个系统权限不知道怎么添加

<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>  

该方法实验没有通过,说是因为没有上面的这个权限,我把该放在manifest中没有效果,后来网上查了一下说是系统权限,自己不会弄就没有继续测试了



你可能感兴趣的:(android,object,String,测试,null,Class)