android 获得一个应用程序的启动次数,运行时间等信息,flutter小程序的onshow
2021/12/13 11:16:59
本文主要是介绍android 获得一个应用程序的启动次数,运行时间等信息,flutter小程序的onshow,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
//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, Ap
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
plicationInfo 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);
这篇关于android 获得一个应用程序的启动次数,运行时间等信息,flutter小程序的onshow的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-13微信小程序如何封装接口域名?-icode9专业技术文章分享
- 2024-11-13如何在微信小程序中实现直传功能?-icode9专业技术文章分享
- 2024-11-13如何在小程序的地图组件中添加标记和文字?-icode9专业技术文章分享
- 2024-11-13在微信小程序的地图组件中如何实现自定义标记和气泡?-icode9专业技术文章分享
- 2024-11-01微信小程序教程:零基础入门到实战
- 2024-11-01微信小程序全栈教程:从入门到实践
- 2024-10-31微信小程序怎么实现关注公众号功能-icode9专业技术文章分享
- 2024-10-30微信小程序cover-view,支持bindtap吗-icode9专业技术文章分享
- 2024-10-30微信小程序的cover-image支持bindtap吗-icode9专业技术文章分享
- 2024-10-30微信小程序web-view怎么设置高度?-icode9专业技术文章分享