本文整理汇总了Java中android.app.usage.UsageStatsManager.queryEvents方法的典型用法代码示例。如果您正苦于以下问题:Java UsageStatsManager.queryEvents方法的具体用法?Java UsageStatsManager.queryEvents怎么用?Java UsageStatsManager.queryEvents使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.app.usage.UsageStatsManager
的用法示例。
在下文中一共展示了UsageStatsManager.queryEvents方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
示例2: 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));
}
}
示例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: 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;
}
示例6: getTargetAppTimeline
import android.app.usage.UsageStatsManager; //导入方法依赖的package包/类
public List<AppItem> getTargetAppTimeline(Context context, String target, int offset) {
List<AppItem> items = new ArrayList<>();
UsageStatsManager manager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
if (manager != null) {
long[] range = AppUtil.getTimeRange(SortEnum.getSortEnum(offset));
UsageEvents events = manager.queryEvents(range[0], range[1]);
UsageEvents.Event event = new UsageEvents.Event();
AppItem item = new AppItem();
item.mPackageName = target;
item.mName = AppUtil.parsePackageName(context.getPackageManager(), target);
// 缓存
ClonedEvent prevEndEvent = null;
long start = 0;
while (events.hasNextEvent()) {
events.getNextEvent(event);
String currentPackage = event.getPackageName();
int eventType = event.getEventType();
long eventTime = event.getTimeStamp();
Log.d("||||------>", currentPackage + " " + target + " " + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault()).format(new Date(eventTime)) + " " + eventType);
if (currentPackage.equals(target)) { // 本次交互开始
Log.d("||||||||||>", currentPackage + " " + target + " " + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault()).format(new Date(eventTime)) + " " + eventType);
// 记录第一次开始时间
if (eventType == UsageEvents.Event.MOVE_TO_FOREGROUND) {
Log.d("********", "start " + start);
if (start == 0) {
start = eventTime;
item.mEventTime = eventTime;
item.mEventType = eventType;
item.mUsageTime = 0;
items.add(item.copy());
}
} else if (eventType == UsageEvents.Event.MOVE_TO_BACKGROUND) { // 结束事件
if (start > 0) {
prevEndEvent = new ClonedEvent(event);
}
Log.d("********", "add end " + start);
}
} else {
// 记录最后一次结束事件
if (prevEndEvent != null && start > 0) {
item.mEventTime = prevEndEvent.timeStamp;
item.mEventType = prevEndEvent.eventType;
item.mUsageTime = prevEndEvent.timeStamp - start;
if (item.mUsageTime <= 0) item.mUsageTime = 0;
if (item.mUsageTime > AppConst.USAGE_TIME_MIX) item.mCount++;
items.add(item.copy());
start = 0;
prevEndEvent = null;
}
}
}
}
return items;
}
示例7: getTopPackageNameAfterLollipop
import android.app.usage.UsageStatsManager; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
private static String getTopPackageNameAfterLollipop(Context context) {
PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
UsageStatsManager usageStatsManager = (UsageStatsManager)context.getSystemService(USAGE_STATS);
if (!powerManager.isInteractive()) {
sForegroundMap.clear();
return null;
}
long time = System.currentTimeMillis();
UsageEvents usageEvents = usageStatsManager.queryEvents(time-5000, time);
if (null == usageEvents) {
return null;
}
String packageName = null;
while (usageEvents.hasNextEvent()) {
UsageEvents.Event event = new UsageEvents.Event();
usageEvents.getNextEvent(event);
packageName = event.getPackageName();
switch (event.getEventType()) {
case UsageEvents.Event.MOVE_TO_FOREGROUND:
sForegroundMap.put(packageName, Long.valueOf(event.getTimeStamp()));
break;
case UsageEvents.Event.MOVE_TO_BACKGROUND:
sForegroundMap.remove(packageName);
break;
}
}
long latest = 0;
for (Map.Entry<String, Long> entry : sForegroundMap.entrySet()) {
long timestamp = entry.getValue().longValue();
if (latest < timestamp) {
packageName = entry.getKey();
latest = timestamp;
}
}
return packageName;
}