本文整理汇总了Java中android.app.usage.UsageStatsManager.queryUsageStats方法的典型用法代码示例。如果您正苦于以下问题:Java UsageStatsManager.queryUsageStats方法的具体用法?Java UsageStatsManager.queryUsageStats怎么用?Java UsageStatsManager.queryUsageStats使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.app.usage.UsageStatsManager
的用法示例。
在下文中一共展示了UsageStatsManager.queryUsageStats方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: retrieveStats
import android.app.usage.UsageStatsManager; //导入方法依赖的package包/类
private void retrieveStats() {
UsageStatsManager manager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
List<UsageStats> stats = manager.queryUsageStats(UsageStatsManager.INTERVAL_BEST,
BEGIN, System.currentTimeMillis());
if (stats == null) {
UILog.i("no stats");
return;
}
for (UsageStats stat : stats) {
String packageName = stat.getPackageName();
int key = mStats.indexOfKey(packageName);
if (key >= 0) {
mStats.valueAt(key).add(stat);
} else {
mStats.put(packageName, new UsageStats(stat));
}
}
}
示例2: isGoRunning
import android.app.usage.UsageStatsManager; //导入方法依赖的package包/类
@Override
public boolean isGoRunning() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
UsageStatsManager usm = (UsageStatsManager) getSystemService(USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - REFRESH_INTERVAL * REFRESH_INTERVAL, time);
if (appList != null && appList.size() > 0) {
SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>();
for (UsageStats usageStats : appList) {
mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
}
if (!mySortedMap.isEmpty()) {
String currentAppName = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
return (currentAppName.equals("com.android.systemui") || currentAppName.equals("com.tomer.poke.notifier") || currentAppName.equals(getPackageName())) ? isGoOpen : currentAppName.equals(Constants.GOPackageName);
}
}
} else {
ActivityManager am = (ActivityManager) getBaseContext().getSystemService(ACTIVITY_SERVICE);
return am.getRunningTasks(1).get(0).topActivity.getPackageName().equals(Constants.GOPackageName);
}
return isGoOpen;
}
示例3: getAppEntriesUsingUsageStats
import android.app.usage.UsageStatsManager; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
private List<AppEntry> getAppEntriesUsingUsageStats() {
UsageStatsManager mUsageStatsManager = (UsageStatsManager) getSystemService(USAGE_STATS_SERVICE);
List<UsageStats> usageStatsList = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_YEARLY, searchInterval, System.currentTimeMillis());
List<AppEntry> entries = new ArrayList<>();
for(UsageStats usageStats : usageStatsList) {
AppEntry newEntry = new AppEntry(
usageStats.getPackageName(),
null,
null,
null,
false
);
newEntry.setTotalTimeInForeground(usageStats.getTotalTimeInForeground());
newEntry.setLastTimeUsed(usageStats.getLastTimeUsed());
entries.add(newEntry);
}
return entries;
}
示例4: isGoRunning
import android.app.usage.UsageStatsManager; //导入方法依赖的package包/类
@Override
public boolean isGoRunning() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
UsageStatsManager usm = (UsageStatsManager) getSystemService(USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - REFRESH_INTERVAL * REFRESH_INTERVAL, time);
if (appList != null && appList.size() > 0) {
SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>();
for (UsageStats usageStats : appList) {
mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
}
if (!mySortedMap.isEmpty()) {
String currentAppName = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
return (currentAppName.equals("com.android.systemui") || currentAppName.equals("com.tomer.poke.notifier") || currentAppName.equals(getPackageName())) ? isGoOpen : currentAppName.equals(Constants.GOPackageName);
}
}
} else {
ActivityManager am = (ActivityManager) getBaseContext().getSystemService(ACTIVITY_SERVICE);
return am.getRunningTasks(1).get(0).topActivity.getPackageName().equals(Constants.GOPackageName);
}
return isGoOpen;
}
示例5: queryUsageStats
import android.app.usage.UsageStatsManager; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static String queryUsageStats() {
String packageName=null;
UsageStatsManager usm = (UsageStatsManager) FingerApp.get().getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,
time - 1000 * 1000, time);
if (appList != null && appList.size() > 0) {
SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
for (UsageStats usageStats : appList) {
mySortedMap.put(usageStats.getLastTimeUsed(),
usageStats);
}
if (!mySortedMap.isEmpty()) {
packageName = mySortedMap.get(
mySortedMap.lastKey()).getPackageName();
}
}
return packageName;
}
示例6: getTaskTopAppPackageName
import android.app.usage.UsageStatsManager; //导入方法依赖的package包/类
/**
* get current task top app package name
* @param context
* @param am
* @return the top app package name
*/
public static String getTaskTopAppPackageName(Context context, ActivityManager am) {
String packageName = "";
// if the sdk >= 21. It can only use getRunningAppProcesses to get task top package name
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager usage = (UsageStatsManager)context.getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List<UsageStats> stats = usage.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 10, time);
if (stats != null) {
SortedMap<Long, UsageStats> runningTask = new TreeMap<Long,UsageStats>();
for (UsageStats usageStats : stats) {
runningTask.put(usageStats.getLastTimeUsed(), usageStats);
}
if (runningTask.isEmpty()) {
return null;
}
packageName = runningTask.get(runningTask.lastKey()).getPackageName();
}
} else {// if sdk <= 20, can use getRunningTasks
List<ActivityManager.RunningTaskInfo> infos = am.getRunningTasks(1);
packageName = infos.get(0).topActivity.getPackageName();
}
return packageName;
}
示例7: getTopActivityAfterLM
import android.app.usage.UsageStatsManager; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
private String getTopActivityAfterLM() {
try {
UsageStatsManager usageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
long milliSecs = 60 * 1000;
Date date = new Date();
List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, date.getTime() - milliSecs, date.getTime());
if (queryUsageStats.size() > 0) {
return null;
}
long recentTime = 0;
String recentPkg = "";
for (int i = 0; i < queryUsageStats.size(); i++) {
UsageStats stats = queryUsageStats.get(i);
if (stats.getLastTimeStamp() > recentTime) {
recentTime = stats.getLastTimeStamp();
recentPkg = stats.getPackageName();
}
}
return recentPkg;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
示例8: isRunningForeground5
import android.app.usage.UsageStatsManager; //导入方法依赖的package包/类
/**
*
* 通过UsageStatsManager来判断
*
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean isRunningForeground5() {
if(Build.VERSION.SDK_INT >= 21){
long ts = System.currentTimeMillis();
UsageStatsManager usageStatsManager = (UsageStatsManager)
getApplicationContext().getSystemService(Context.USAGE_STATS_SERVICE);
List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(
UsageStatsManager.INTERVAL_BEST, ts - 2000, ts);
if (queryUsageStats == null || queryUsageStats.isEmpty()) {
return false;
}
UsageStats recentStats = null;
for (UsageStats usageStats : queryUsageStats) {
if (recentStats == null ||
recentStats.getLastTimeUsed() < usageStats.getLastTimeUsed())
{
recentStats = usageStats;
}
}
if(getPackageName().equals(recentStats.getPackageName())){
return true;
}
}
return false;
}
示例9: getUsageStats
import android.app.usage.UsageStatsManager; //导入方法依赖的package包/类
@SuppressWarnings("ResourceType")
@Nullable
private SortedMap<Long, UsageStats> getUsageStats(@NonNull Context context)
throws SecurityException {
UsageStatsManager usm = (UsageStatsManager) context.getSystemService(USAGE_STATS_MANAGER);
// We get usage stats for the last 30 seconds
final long timeEnd = System.currentTimeMillis();
final long timeBegin = timeEnd - 30 * 1000; // +30 sec.
List<UsageStats> statsList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,
timeBegin,
timeEnd);
if (statsList != null) {
// Sort the stats by the last time used
SortedMap<Long, UsageStats> statsSortedMap = new TreeMap<>();
for (final UsageStats usageStats : statsList) {
// Filter system decor apps
if ("com.android.systemui".equals(usageStats.getPackageName())) {
continue;
}
statsSortedMap.put(usageStats.getLastTimeUsed(), usageStats);
}
return statsSortedMap;
}
return null;
}
示例10: getTopApp
import android.app.usage.UsageStatsManager; //导入方法依赖的package包/类
public static String getTopApp(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // 5.0及之后的方法
UsageStatsManager usm = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
if (usm != null) {
long now = System.currentTimeMillis();
// 获取40秒之内的应用程序使用状态
List<UsageStats> stats = usm.queryUsageStats(UsageStatsManager.INTERVAL_BEST, now - 40 * 1000, now);
String topActivity = "";
// 获取最新运行的程序
if ((stats != null) && (!stats.isEmpty())) {
int j = 0;
for (int i = 0; i < stats.size(); i++) {
if (stats.get(i).getLastTimeUsed() > stats.get(j).getLastTimeUsed()) {
j = i;
}
}
topActivity = stats.get(j).getPackageName();
return topActivity;
}
}
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){ // 5.0之前的方法
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = activityManager.getRunningTasks(1).get(0).topActivity;
return cn.getPackageName();
}
return "Not found!";
}
示例11: currentPackage
import android.app.usage.UsageStatsManager; //导入方法依赖的package包/类
private String currentPackage() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager usm = (UsageStatsManager) getSystemService(USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,
time - 1000 * 1000, time);
if (appList != null && appList.size() > 0) {
SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
for (UsageStats usageStats : appList) {
mySortedMap.put(usageStats.getLastTimeUsed(),
usageStats);
}
if (mySortedMap != null && !mySortedMap.isEmpty()) {
return mySortedMap.get(
mySortedMap.lastKey()).getPackageName();
}
}
}
return currentTask().topActivity.getPackageName();
}
示例12: getCurrentAppUsingUsageStats
import android.app.usage.UsageStatsManager; //导入方法依赖的package包/类
@Nullable
public static String getCurrentAppUsingUsageStats(Context context) {
try {
if (Build.VERSION.SDK_INT >= 21) {
// Although the UsageStatsManager was added in API 21, the
// constant to specify it wasn't added until API 22.
// So we use the value of that constant on API 21.
String usageStatsServiceString;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
usageStatsServiceString = (atLeastAPI(22)) ? Context.USAGE_STATS_SERVICE : "usagestats";
} else {
usageStatsServiceString = "usagestats";
}
@SuppressWarnings("WrongConstant")
UsageStatsManager usm = (UsageStatsManager) context.getSystemService(usageStatsServiceString);
long time = System.currentTimeMillis();
List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time);
if (appList != null && appList.size() > 0) {
TreeMap<Long, UsageStats> mySortedMap = new TreeMap<>();
for (UsageStats usageStats : appList) {
mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
}
if (!mySortedMap.isEmpty()) {
return mySortedMap.get(mySortedMap.lastKey()).getPackageName();
}
}
}
} catch (Exception e) {
// Ignore exceptions to allow the user to determine if it
// works him/herself
}
return null;
}
示例13: getForegroundAppPackageName
import android.app.usage.UsageStatsManager; //导入方法依赖的package包/类
@SuppressWarnings("WrongConstant")
public static String getForegroundAppPackageName(Context context) {
UsageStatsManager manager = (UsageStatsManager) context.getSystemService("usagestats");
long time = System.currentTimeMillis();
List<UsageStats> list = manager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,
time - 1000 * 1000, time);
if (list != null && !list.isEmpty()) {
SortedMap<Long, UsageStats> map = new TreeMap<>();
for (UsageStats stats : list) {
map.put(stats.getLastTimeUsed(), stats);
}
if (!map.isEmpty()) {
return map.get(map.lastKey()).getPackageName();
}
}
return null;
}
示例14: getPackageUsageStats
import android.app.usage.UsageStatsManager; //导入方法依赖的package包/类
/**
* 获取系统中所有应用的使用数据,不一定能读全
* @param context
* @return
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static List<UsageStats> getPackageUsageStats(Context context){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
long endt = calendar.getTimeInMillis();//结束时间
calendar.add(Calendar.DAY_OF_YEAR, -2);//时间间隔为两年
long statt = calendar.getTimeInMillis();//开始时间
UsageStatsManager usageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
List<UsageStats> queryUsageStats = null;
try {
queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_YEARLY, statt, endt);
} catch (Throwable e) {
// 360 1509-A00 6.0.1(23)、QiKU 8676-A01 5.1(22)、360 1505-A02 6.0.1(23)
// java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.os.IBinder.transact(int, android.os.Parcel, android.os.Parcel, int)' on a null object reference
// at android.content.pm.ParceledListSlice.(ParceledListSlice.java:97)
// at android.content.pm.ParceledListSlice.(ParceledListSlice.java:41)
// at android.content.pm.ParceledListSlice$2.createFromParcel(ParceledListSlice.java:200)
// at android.content.pm.ParceledListSlice$2.createFromParcel(ParceledListSlice.java:198)
// at android.app.usage.IUsageStatsManager$Stub$Proxy.queryUsageStats(IUsageStatsManager.java:184)
// at android.app.usage.UsageStatsManager.queryUsageStats(UsageStatsManager.java:133)
// at com.qihoo.utils.PackageUsageHelper.getPackageUsageStats(AppStore:161)
// at com.qihoo.utils.PackageUsageHelper.getPackageUsageTime(AppStore:188)
// at com.qihoo.express.mini.support.ApkWatcherData.getApkShowTimes(AppStore:87)
// at com.qihoo.appstore.localapkinfo.LocalApkInfoDb.initLocalApkSimpleInfo(AppStore:268)
// at com.qihoo.appstore.localapkinfo.LocalApkInfoDb.LoadAllSimpleApkInfo(AppStore:314)
// at com.qihoo.appstore.localapkinfo.LocalApkInfoDb.loadAllApkInfo(AppStore:100)
// at com.qihoo.appstore.localapkinfo.LocalApkMgr$5.run(AppStore:442)
// at com.qihoo.utils.thread.LooperThread.run(AppStore:38)
if (LogUtils.isDebug()) {
LogUtils.e(PackageUsageHelper.class.getName(), "getPackageUsageStats", e);
}
}
return queryUsageStats;
}else{
return null;
}
}
示例15: getRunningApp
import android.app.usage.UsageStatsManager; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Nullable
private static String getRunningApp(Context mContext) {
String topPackageName = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//noinspection WrongConstant
UsageStatsManager mUsageStatsManager = (UsageStatsManager) mContext.getSystemService("usagestats");
long time = System.currentTimeMillis();
// We get usage stats for the last 10 seconds
List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 10, time);
// Sort the stats by the last time used
if (stats != null) {
SortedMap<Long, String> mySortedMap = new TreeMap<>();
for (UsageStats usageStats : stats) {
mySortedMap.put(usageStats.getLastTimeUsed(), usageStats.getPackageName());
}
if (!mySortedMap.isEmpty()) {
do {
topPackageName = mySortedMap.get(mySortedMap.lastKey());
if (mContext.getPackageName().equals(topPackageName) || "android".equals(topPackageName)) {
mySortedMap.remove(mySortedMap.lastKey());
if (mySortedMap.isEmpty())
break;
} else {
break;
}
} while (true);
}
}
}
return topPackageName;
}