Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

方法4,queryUsageStats偶尔返回空集合,导致return false认为并不在前台(其实在) #17

Open
Drinkwatermore opened this issue Feb 3, 2017 · 3 comments

Comments

@Drinkwatermore
Copy link

我这样改了下就好了。
原:
/*
if (usageStats == null || usageStats.size() == 0) {
if (HavaPermissionForTest(context) == false) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Toast.makeText(context, "权限不够\n请打开手机设置,点击安全-高级,在有权查看使用情况的应用中,为这个App打上勾", Toast.LENGTH_SHORT).show();
}
//发现造成这里的false 是因为 usageStats.size() == 0,即空集合(实际手机有任务存在)
// boolean r = (usageStats == null);
//android.util.Log.e("Current","usageStats:null:"+r);

        return false;
    }*/

改后:
//改一下上面的
if (usageStats == null || usageStats.size() == 0) {
if (HavaPermissionForTest(context) == false) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

            //1.我是子线程里使用Toast
            Looper.prepare();
            Toast.makeText(context, "权限不够\n请打开手机设置,点击安全-高级,在有权查看使用情况的应用中,为这个App打上勾", Toast.LENGTH_SHORT).show();
            Looper.loop();// 进入loop中的循环,查看消息队列

            //2.添加这里,
            return false;
        }
        //3.一定得返回,不然后面会下标越界,估计返回true,一直检查的话,马上就会有真实情况
        return true;
    }
@wenmingvs
Copy link
Owner

wenmingvs commented Feb 3, 2017 via email

@xingstarx
Copy link

我也碰到过类似的这种问题,之前的做法是保存usageStats.get(0),因为在某些rom上,长时间停留的话,获取到的usageStats是空List,这个时候我们只需要取上一次保存的usageStats.get(0)即可,然后在进行判断。

@fr0zenrain
Copy link

上一次?时间怎么控制

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants