gpt4 book ai didi

android - 无法解析符号 USAGE_STATS_SERVICE

转载 作者:行者123 更新时间:2023-11-30 01:29:41 26 4
gpt4 key购买 nike

我引用了一些论坛和讨论并编写了这段代码来查找运行最频繁的 Activity 并开始另一个 Activity 的 Intent 。

package com.example.neondude.ietproject;

import android.app.Service;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;

import java.util.List;
import java.util.SortedMap;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeMap;


public class BlockService extends Service
{
private static Timer timer = new Timer();
public String pActivity="";




public IBinder onBind(Intent arg0)
{
return null;
}

public void onCreate()
{
super.onCreate();
startService();
}

private void startService()
{
timer.scheduleAtFixedRate(new mainTask(), 0, 500);
}

private class mainTask extends TimerTask
{
public void run()
{
toastHandler.sendEmptyMessage(0);
}
}

public void onDestroy()
{
super.onDestroy();
Toast.makeText(this, "Service Stopped ...", Toast.LENGTH_SHORT).show();
}

private final Handler toastHandler = new Handler()
{

@Override
public void handleMessage(Message msg) {
String topPackageName = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

UsageStatsManager mUsageStatsManager = (UsageStatsManager) getSystemService(USAGE_STATS_SERVICE);
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, UsageStats> mySortedMap = new TreeMap<>();
for (UsageStats usageStats : stats) {
mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
}
if (!mySortedMap.isEmpty()) {
topPackageName = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
}

}
}
if (!topPackageName.equals("com.example.neondude.MainActivity") || !topPackageName.equals("com.example.neondude.BlockService")) {
Intent i = new Intent(BlockService.this, BlockActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
Toast.makeText(BlockService.this, pActivity, Toast.LENGTH_LONG).show();

}
}
};
}

但我在使用 USAGE_STATS_SERVICE 时遇到问题

UsageStatsManager mUsageStatsManager = (UsageStatsManager) getSystemService(USAGE_STATS_SERVICE);

错误:无法解析符号 USAGE_STATS_SERVICE

我也尝试过使用:

UsageStatsManager mUsageStatsManager = (UsageStatsManager) getSystemService("usagestats");

这也显示了同样的错误

请告诉我如何更正此错误。

最佳答案

确保您的 compileSdkVersion 设置为 22 或更高,因为这是 API 级别 where USAGE_STATS_SERVICE was added .

关于android - 无法解析符号 USAGE_STATS_SERVICE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35943146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com