new Thread(new Runnable(){
@Override
public void run() {
PackageManager pckMan = getPackageManager();
List<PackageInfo> packs = pckMan.getInstalledPackages(0);
ArrayList<HashMap<String, Object>> item = new ArrayList<HashMap<String, Object>>();
for (PackageInfo p:packs) {
Log.d(TAG, "---p.packageName = " + p.packageName);
}
Intent i = pckMan.getLaunchIntentForPackage("com.mobilead.mvp");
ComponentName cp = i.getComponent();
int lunchCount = 0;
long useTime = 0;
try {
Class<?> ServiceManager = Class.forName("android.os.ServiceManager");
Method getService = ServiceManager.getMethod("getService", java.lang.String.class);
Log.d(TAG, "---- getService = " + getService.toString());
Object oRemoteService = getService.invoke(null, "usagestats");
Class<?> cStub = Class.forName("com.android.internal.app.IUsageStats$Stub");
Method asInterface = cStub.getMethod("asInterface", android.os.IBinder.class);
Log.d(TAG, "---- asInterface = " + asInterface.toString());
Object oIUsageStats = asInterface.invoke(null, oRemoteService);
Method getPkgUsageStats = oIUsageStats.getClass().getMethod("getPkgUsageStats", ComponentName.class);
Log.d(TAG, "---- getPkgUsageStats = " + getPkgUsageStats.toString());
Object aStats = getPkgUsageStats.invoke(oIUsageStats, cp);
Log.d(TAG, "---- aStats = " + aStats.toString());
Class<?> PkgUsageStats = Class.forName("com.android.internal.os.PkgUsageStats");
lunchCount = PkgUsageStats.getDeclaredField("launchCount").getInt(aStats);
useTime = PkgUsageStats.getDeclaredField("usageTime").getLong(aStats);
Log.d(TAG, "---- lunchCount = " + lunchCount + " useTime = " + useTime);
} catch (Exception e) {
Log.d(TAG, "---- e = " + e);
}
}
}).start();
获取应用的使用次数和使用时间
最新推荐文章于 2024-03-30 17:46:29 发布