2

我已经迁移了我的 Android 应用程序,试试 Android O (26)。

我已在 Android Beta 计划中注册了我支持的设备,并且我正在 Google PIXEL XL (OPP3.170518.006) 上进行测试

我的应用程序中的功能需要“显示在其他应用程序上”。

工艺流程如下:-

1)。用户尝试使用需要“显示在其他应用程序上”的功能,但尚未获得批准。

2)。活动请求权限如下:-

Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,                            Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, RC_PERMISSION_MANAGE_OVERLAY_PERMISSION);

3)。在onActivityResult我检查用户授予的权限并启动我的关联进程。

在迁移到 Android O 之前,这一系列事件运行良好。

我现在可以让它工作的唯一方法是在 onActivityResult() 中使用 postDelay 为 3 秒的 Handler 来启动我的关联进程。

如何立即检测授予的权限而不必等待 3 秒?

我开发了以下研究应用程序来说明这个问题

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_physical);

    img = findViewById(R.id.spring_button);
    img.setOnClickListener(this);

    Log.d(TAG, "onCreate: " + Settings.canDrawOverlays(Physical.this));
}

@Override
public void onClick(View view) {

    final Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent, RC_PERMISSION_MANAGE_OVERLAY_PERMISSION);

}

@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    Log.d(TAG, "onPostCreate: " + Settings.canDrawOverlays(Physical.this));
}

@Override
protected void onPostResume() {
    super.onPostResume();
    Log.d(TAG, "onPostResume: " + Settings.canDrawOverlays(Physical.this));
}

@Override
protected void onStart() {
    super.onStart();
    Log.d(TAG, "onStart: " + Settings.canDrawOverlays(Physical.this));
}

@Override
protected void onStop() {
    super.onStop();
    Log.d(TAG, "onStop: " + Settings.canDrawOverlays(Physical.this));
}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroy: " + Settings.canDrawOverlays(Physical.this));
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    Log.d(TAG, "onBackPressed: " + Settings.canDrawOverlays(Physical.this));
}

@Override
protected void onPause() {
    super.onPause();
    Log.d(TAG, "onPause: " + Settings.canDrawOverlays(Physical.this));
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Log.d(TAG, "onNewIntent: " + Settings.canDrawOverlays(Physical.this));
}

@Override
protected void onResume() {
    super.onResume();
    Log.d(TAG, "onResume: " + Settings.canDrawOverlays(Physical.this));
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case RC_PERMISSION_MANAGE_OVERLAY_PERMISSION:
            Log.d(TAG, "onActivityResult: " + Settings.canDrawOverlays(Physical.this));
            break;
    }
}

Logcat 输出如下:-

D/Physical: onCreate: true
D/Physical: onStart: true
D/Physical: onPostCreate: true
D/Physical: onResume: true
D/Physical: onPostResume: true

D/Physical: onPause: true
D/Physical: onStop: false
D/Physical: onActivityResult: false
D/Physical: onStart: false
D/Physical: onResume: false
D/Physical: onPostResume: false

我的应用程序已被授予“ ”权限,例如与“ ”Display over other apps相关的拨动开关在生成这些日志的整个过程中都是如此。Allow display over other appsON

那么为什么在, , ,和中调用Settings.canDrawOverlays(Physical.this)return false呢?onStoponActivityResultonStartonResumeonPostResume

4

0 回答 0