无法解析符号USAGE

无法解析符号USAGE

我已经参考了一些论坛和讨论,并编写了这段代码来查找运行最频繁的活动并开始进行其他活动。

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/

10-11 19:53