为什么“ usageStatsManager.queryUsageStats()”返回大小为0的列表?

时间:2018-12-28 14:38:25

标签: java android

我制作了一个小应用程序,可以使用我的应用程序,但是当我将我的代码带到我的真实应用程序中时,ıts不会返回任何内容。(我的旧应用程序最小SDK版本= 15新版本= API 23:Android 6.0)

/我的第一个应用

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
            //startActivity(intent);
            //GetStatus();
            TextView textView = (TextView) findViewById(R.id.text_view);
            textView.setText("APP USTAGE");
            Button button= (Button)findViewById(R.id.But_3);
            button.setOnClickListener(this);

            }
        }

    @Override
    public void onClick(View v)
    {
        int HoursBefore = -2;
        System.out.println("1--------------------------");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            EditText tmpx = findViewById(R.id.editText2);
            HoursBefore = parseInt(String.valueOf(tmpx.getText()));
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            GetStatus(HoursBefore);

        System.out.println(" saat degeri : " + HoursBefore);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public  void  GetStatus(int HoursBefore){
        final UsageStatsManager usageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);// Context.USAGE_STATS_SERVICE);

        Calendar StartCla = Calendar.getInstance();
        StartCla.add(Calendar.HOUR, -HoursBefore);
        Calendar EndCla = Calendar.getInstance();

        final List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, StartCla.getTimeInMillis(), EndCla.getTimeInMillis());
        System.out.println("\n\n--------------------------------------------------------------------------------------------------------------------------------------------\n\n");

        String res = "";
        Date time1 , time2 ;
        for (UsageStats app : queryUsageStats) {
            if((float) (app.getTotalTimeInForeground() / 1000) != 0){
                System.out.println("-------> "+app.getPackageName() + " | " + (float) (app.getTotalTimeInForeground() / 1000));
                time1 = new java.util.Date(app.getLastTimeStamp());
                time2 = new java.util.Date(app.getLastTimeStamp()-(app.getTotalTimeInForeground()));
                res += app.getPackageName() +" -> \n"+
                        time1.getHours()+ " : " + time1.getMinutes() + ":" + time1.getSeconds() + " |-| " + time2.getHours()+ " : " + time2.getMinutes() + ":" + time2.getSeconds()+ "\n"+
                        + time1.getDay() + "." + time1.getMonth()+"."+( 1900 + time1.getYear())  + " |-| " + time2.getDay() + "." + time2.getMonth()+"."+( 1900 + time2.getYear()) + "\n"
                        + " -- " + app.getTotalTimeInForeground() / 1000 + "\n";
            }
        }
        System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------\n\n");

        TextView textView = (TextView) findViewById(R.id.text_view);
        textView.setText(res);
        textView.setMovementMethod(new ScrollingMovementMethod());

    }

}

/我的2.应用

public class OperationCenter {

    public static int RaportTimeHour = 10;//           add to the save method
    private Context cn;

    public OperationCenter(Context con){
        cn = con;
        ProgramData.TakeWorkingDic(con.getFilesDir().toString());
        LoadApp();
    }

    private boolean CheckNetworkStatus(){

        return  true;
    }

    public void TakeRaport(){
        TakeAppUsageData();
    }

    public void  StartSpyService(){

    }

    public void StopSpyService(){

    }

    private UsageRaport TakeAppUsageData(){
        final UsageStatsManager usageStatsManager = (UsageStatsManager) cn.getSystemService(Context.USAGE_STATS_SERVICE);// Context.USAGE_STATS_SERVICE);
        Calendar StartCla = Calendar.getInstance();
        StartCla.add(Calendar.HOUR, -RaportTimeHour);
        Calendar EndCla = Calendar.getInstance();

        final List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, StartCla.getTimeInMillis(), EndCla.getTimeInMillis());

        String tmp = StartCla.getTime().getHours() + " - " + EndCla.getTime().getHours() ;
        Log.i("rap",tmp);
        String res = "";
        Date time1 , time2 ;
        for (UsageStats app : queryUsageStats){
            if((float) (app.getTotalTimeInForeground() / 1000) != 0){
                time1 = new java.util.Date(app.getLastTimeStamp());
                time2 = new java.util.Date(app.getLastTimeStamp()-(app.getTotalTimeInForeground()));
                Log.i("rap",app.getPackageName() +" -> \n"+
                        time1.getHours()+ " : " + time1.getMinutes() + ":" + time1.getSeconds() + " |-| " + time2.getHours()+ " : " + time2.getMinutes() + ":" + time2.getSeconds()+ "\n"+
                        + time1.getDay() + "." + time1.getMonth()+"."+( 1900 + time1.getYear())  + " |-| " + time2.getDay() + "." + time2.getMonth()+"."+( 1900 + time2.getYear()) + "\n"
                        + " -- " + app.getTotalTimeInForeground() / 1000 + "\n");
            }
        }

        Log.i("rap","---------------------------------------------------------------------------------------------------");
        //Log.i("rap",res);
        Log.i("rap","---------------------------------------------------------------------------------------------------");

        return null;
    }

    public void SaveApp(){

    }

    public void LoadApp(){
        ProgramData.LoadMyData();

    }

}

0 个答案:

没有答案