gpt4 book ai didi

具有 PACKAGE_USAGE_STATS 权限的 AndroidTest

转载 作者:行者123 更新时间:2023-11-30 00:43:48 33 4
gpt4 key购买 nike

我们的目标是使用 AndroidTest(AndroidJUnit4) 运行自动测试。

当 SDK >= 23 时,我们已使用此代码授予权限

public static void grantPermission(String permission) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!hasPermission(permission))
getInstrumentation().getUiAutomation()
.executeShellCommand("pm grant " + getTargetContext().getPackageName() + " " + permission);
}
}

@Before
public void setUp() throws Exception {
grantPermission("android.permission.ACCESS_NETWORK_STATE");
grantPermission("android.permission.CHANGE_NETWORK_STATE");
grantPermission("android.permission.CAMERA ");
grantPermission("android.permission.INTERNET");
grantPermission("android.permission.READ_EXTERNAL_STORAGE ");
grantPermission("android.permission.WRITE_EXTERNAL_STORAGE");
grantPermission("android.permission.PACKAGE_USAGE_STATS");
}

该方法适用于

android.permission.READ_EXTERNAL_STORAGE
android.permission.WRITE_EXTERNAL_STORAGE
android.permission.CAMERA

虽然在Settings/Security/Apps with usage access中,我们的app是开启的,但是当我们使用这段代码检查权限时,它仍然没有那个权限。

AppOpsManager appOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);

// return MODE_DEFAULT, not MODE_ALLOWED
int result = appOps.checkOpNoThrow("android:get_usage_stats", android.os.Process.myUid(), context.getPackageName())

我怎样才能实现这个目标?

最佳答案

我们已经尝试了许多认可的。最后,我们成功了。

批准的是使用“adb shell input tap [x], [y]”来模拟点击,所以就像人类点击屏幕触发开关一样。

这是我们使用的代码。

private void grantPermission() throws InterruptedException {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Context context = InstrumentationRegistry.getTargetContext();
final AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);

// check if the app doesn't have permission
if (appOps.checkOpNoThrow("android:get_usage_stats", android.os.Process.myUid(), context.getPackageName()) != AppOpsManager.MODE_ALLOWED) {
UiAutomation automation = InstrumentationRegistry.getInstrumentation().getUiAutomation();

//Open UsageAccessSettingsActivity
automation.executeShellCommand("am start com.android.settings/.Settings$UsageAccessSettingsActivity");
Thread.sleep(1000);

//Open the setting of the first app
automation.executeShellCommand(String.format("input tap %s %s", dpToPx(100), dpToPx(138)));
Thread.sleep(1000);

//Tap permit usage access
automation.executeShellCommand(String.format("input tap %s %s", dpToPx(100), dpToPx(164)));
Thread.sleep(1000);
}
}
}

public static int dpToPx(int dp) {
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}

我们已经在 SDK 23 和 24 上使用 hdip、xhdip 和 xxhdip 尝试了 6 个 AVD。所有作品。

更新[2/10]:

我们找到了另一种更简单的方法。使用“adb shell appops”推荐。这是我们的代码。

@Before
public void setUp() throws Exception {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Context context = InstrumentationRegistry.getTargetContext();
final AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);

if (appOps.checkOpNoThrow("android:get_usage_stats", android.os.Process.myUid(), context.getPackageName()) != AppOpsManager.MODE_ALLOWED) {
InstrumentationRegistry
.getInstrumentation()
.getUiAutomation()
.executeShellCommand("appops set " + context.getPackageName() + " android:get_usage_stats allow");
}
}
}

关于具有 PACKAGE_USAGE_STATS 权限的 AndroidTest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42118087/

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