|
|
@@ -1,10 +1,12 @@
|
|
|
package com.sheep.gamegroup.util;
|
|
|
|
|
|
import android.app.Activity;
|
|
|
+import android.content.ComponentName;
|
|
|
import android.content.Intent;
|
|
|
import android.content.pm.ApplicationInfo;
|
|
|
import android.content.pm.PackageInfo;
|
|
|
import android.content.pm.PackageManager;
|
|
|
+import android.content.pm.ResolveInfo;
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
import android.os.Build;
|
|
|
import android.provider.AlarmClock;
|
|
|
@@ -34,6 +36,7 @@ import java.io.BufferedReader;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStreamReader;
|
|
|
+import java.lang.reflect.Method;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Calendar;
|
|
|
@@ -314,6 +317,78 @@ public class SysAppUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public static ComponentName getAppComponentName(String packageName) {
|
|
|
+
|
|
|
+ // 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
|
|
|
+ Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
|
|
|
+ resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
|
|
|
+ resolveIntent.setPackage(packageName);
|
|
|
+
|
|
|
+ // 通过getPackageManager()的queryIntentActivities方法遍历
|
|
|
+ List<ResolveInfo> resolveinfoList = SheepApp.getInstance().getPackageManager()
|
|
|
+ .queryIntentActivities(resolveIntent, 0);
|
|
|
+
|
|
|
+ ResolveInfo resolveinfo = null;
|
|
|
+ try {
|
|
|
+ resolveinfo = resolveinfoList.iterator().next();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ if (resolveinfo != null) {
|
|
|
+ // 这个就是我们要找的该APP的LAUNCHER的Activity[组织形式:packagename.mainActivityname]
|
|
|
+ String className = resolveinfo.activityInfo.name;
|
|
|
+ // LAUNCHER Intent
|
|
|
+ Intent intent = new Intent(Intent.ACTION_MAIN);
|
|
|
+ intent.addCategory(Intent.CATEGORY_LAUNCHER);
|
|
|
+ // 设置ComponentName参数1:packagename参数2:MainActivity路径
|
|
|
+ ComponentName cn = new ComponentName(packageName, className);
|
|
|
+ return cn;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ public static void getAppUsageStats(ComponentName aName) {
|
|
|
+ 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);
|
|
|
+
|
|
|
+ //获得PkgUsageStats类
|
|
|
+ Class<?> PkgUsageStats = Class.forName("com.android.internal.os.PkgUsageStats");
|
|
|
+
|
|
|
+ int aLaunchCount = PkgUsageStats.getDeclaredField("launchCount").getInt(aStats);
|
|
|
+ long aUseTime = PkgUsageStats.getDeclaredField("usageTime").getLong(aStats);
|
|
|
+
|
|
|
+ LogUtil.println("getAppUsageStats", aLaunchCount, aUseTime);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ LogUtil.println("getAppUsageStats", "Exception", e.toString(), e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 显示升级对话框
|
|
|
* @param activity
|