问题
I am building and Android app and trying to learn all I can in the process. As a test I wanted to add a "chat heads" style overlay to the system. So, I looked around and found some good tutorials, and some really good answers on here, but it is still not working. In my android manifest I have the uses-permission declaration..
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Then in my Preference page, when the user clicks on a SwitchPreference to activate the popup chat window I am checking for version and permissions like so...
private Preference.OnPreferenceChangeListener chatHeadsChangeListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
if((boolean) o) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
!Settings.canDrawOverlays(getApplicationContext())) {
Intent intent = new Intent(
Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName())
);
startActivityForResult(intent, CODE_DRAW_OVER_OTHER_APP_PERMISSION);
} else {
initializeChatHeadsView();
}
}
return true;
}
};
Then in my onActivityResult I am checking again to make sure, like one answer I found on here...
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CODE_DRAW_OVER_OTHER_APP_PERMISSION) {
if(Settings.canDrawOverlays(this)) {
initializeChatHeadsView();
} else {
Toast.makeText(
this,
"Draw over other app permission was not available. Cannot activate Popup Chat",
Toast.LENGTH_LONG
).show();
}
}
}
The first time I load the app up on the emulator, which is running Android 8.0, I go into my settings page and click the SwitchPreference turning it on. The settings page opens up asking me if I want to enable the permission, I activate the permission, but it doesn't go back to my app after activating, so I hit the back button which goes back into my app, then I get the "[Application] has stopped responding." If I check the log in AndroidStudio I get the error...
Process: test.notreal.justatext, PID: 21429
java.lang.RuntimeException: Unable to create service test.notreal.justatext.service.FloatingViewService: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@bc855eb -- permission denied for window type 2002
So it is still saying the permission is denied, even though I am enabling the permission, and in my onActivityResult I am checking if(Settings.canDrawOverlays())
Can anyone see why this is failing? I can't figure it out. Thank you for any help, I really appreciate it.
回答1:
for apps targeting Android 8.0
apps must use a new window type called TYPE_APPLICATION_OVERLAY.
so for Android 8.0 you must ask this permission, for lower verison
SYSTEM_ALERT_WINDOW permission
will work
For more Refer this link
https://developer.android.com/about/versions/oreo/android-8.0-changes.html#cwt
来源:https://stackoverflow.com/questions/47143505/android-system-overlay-window-not-working