I'm working on an App that is uses the SYSTEM_ALERT_WINDOW permission. I've added the permission to the Manifest.
In my activity, I've added the following code to ask permission at runtime.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
startActivityForResult(myIntent, 5469);
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 5469: {
Toast.makeText(this,"Permission Granted!",Toast.LENGTH_SHORT).show();
return;
}
}
}
But, as the "Draw over other Apps" setting page is opened, the 'switch button' to allow the permission is greyed out(not clickable).
Given below is the screenshot :
Code for Manifest :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.graphics.unlockbrtest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="Settings"
android:theme="@style/AppTheme.NoActionBar" />
<receiver
android:name=".UnlockReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<activity
android:name=".FloatingActivity"
android:theme="@android:style/Theme.Dialog" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
</application>
What am I doing wrong that the option is greyed out? Thanks in advance!
Move your <uses-permission>
element to be an immediate child of <manifest>
, outside of the <application>
element.