本文整理汇总了Java中android.app.usage.UsageStatsManager类的典型用法代码示例。如果您正苦于以下问题:Java UsageStatsManager类的具体用法?Java UsageStatsManager怎么用?Java UsageStatsManager使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
UsageStatsManager类属于android.app.usage包,在下文中一共展示了UsageStatsManager类的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: getForegroundPackage
import android.app.usage.UsageStatsManager; //导入依赖的package包/类
public static String getForegroundPackage(UsageStatsManager usageStatsManager) {
String packageName = null;
final long INTERVAL = 1000 * 60;
final long end = System.currentTimeMillis();
final long begin = end - INTERVAL;
final UsageEvents usageEvents = usageStatsManager.queryEvents(begin, end);
while (usageEvents.hasNextEvent()) {
UsageEvents.Event event = new UsageEvents.Event();
usageEvents.getNextEvent(event);
switch (event.getEventType()) {
case UsageEvents.Event.MOVE_TO_FOREGROUND:
packageName = event.getPackageName();
break;
case UsageEvents.Event.MOVE_TO_BACKGROUND:
if (event.getPackageName().equals(packageName)) {
packageName = null;
}
}
}
return packageName;
}
示例3: getForegroundApp
import android.app.usage.UsageStatsManager; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public String getForegroundApp(final Context context) {
if(!Utils.hasUsageStatsPermission(context))
return null;
String foregroundApp = null;
UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Service.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
UsageEvents usageEvents = mUsageStatsManager.queryEvents(time - 1000 * 3600, time);
UsageEvents.Event event = new UsageEvents.Event();
while (usageEvents.hasNextEvent()) {
usageEvents.getNextEvent(event);
if(event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) {
foregroundApp = event.getPackageName();
}
}
return foregroundApp ;
}
示例4: getStats
import android.app.usage.UsageStatsManager; //导入依赖的package包/类
@SuppressWarnings("ResourceType")
public static void getStats(Context context){
UsageStatsManager usm = (UsageStatsManager) context.getSystemService("usagestats");
int interval = UsageStatsManager.INTERVAL_YEARLY;
Calendar calendar = Calendar.getInstance();
long endTime = calendar.getTimeInMillis();
calendar.add(Calendar.YEAR, -1);
long startTime = calendar.getTimeInMillis();
Log.d(TAG, "Range start:" + dateFormat.format(startTime) );
Log.d(TAG, "Range end:" + dateFormat.format(endTime));
UsageEvents uEvents = usm.queryEvents(startTime,endTime);
while (uEvents.hasNextEvent()){
UsageEvents.Event e = new UsageEvents.Event();
uEvents.getNextEvent(e);
if (e != null){
Log.d(TAG, "Event: " + e.getPackageName() + "\t" + e.getTimeStamp());
}
}
}
示例5: 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;
}
示例6: 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;
}
示例7: 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;
}
示例8: 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;
}
示例9: 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;
}
示例10: updateRecentApps
import android.app.usage.UsageStatsManager; //导入依赖的package包/类
private void updateRecentApps() {
Blacklist blacklist = Blacklist.getInstance(this);
String currentForegroundApp = null;
if(!U.isNightModeOn(this))
stopSelf();
else if(Settings.Secure.getInt(getContentResolver(), "accessibility_display_inversion_enabled", 0) == 1)
U.setTint(this, false);
else {
if(blacklist.getBlockedApps().size() > 0) {
UsageStatsManager mUsageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
UsageEvents events = mUsageStatsManager.queryEvents(
System.currentTimeMillis() - AlarmManager.INTERVAL_DAY,
System.currentTimeMillis());
UsageEvents.Event eventCache = new UsageEvents.Event();
while(events.hasNextEvent()) {
events.getNextEvent(eventCache);
if(eventCache.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND)
currentForegroundApp = eventCache.getPackageName();
}
}
U.setTint(this, !blacklist.isBlocked(currentForegroundApp));
}
}
示例11: 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 "";
}
示例12: 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;
}
示例13: 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;
}
示例14: doInBackground
import android.app.usage.UsageStatsManager; //导入依赖的package包/类
@Override
protected List<UsageEventsItem> doInBackground(Void... params) {
//noinspection ResourceType
UsageStatsManager usageStatsMgr = (UsageStatsManager) getSystemService("usagestats");
UsageEvents events = usageStatsMgr.queryEvents(mStartTime, mEndTime);
List<UsageEventsItem> results = new ArrayList<>();
UsageEvents.Event event = new UsageEvents.Event();
PackageManager pm = getPackageManager();
while (events.getNextEvent(event)) {
UsageEventsItem item = new UsageEventsItem();
item.pkgName = event.getPackageName();
item.className = event.getClassName();
item.type = event.getEventType();
item.timeStamp = event.getTimeStamp();
item.appName = item.pkgName;
try {
item.appName = pm.getApplicationInfo(item.pkgName, 0).loadLabel(pm).toString();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
results.add(item);
}
Collections.sort(results, new UsageEventsItem.AppNameComparator());
return results;
}
示例15: 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!";
}