Click here to Skip to main content
15,885,546 members
Articles / Mobile Apps / Android
Tip/Trick

Android Marshmallow Overlay Permission

Rate me:
Please Sign up or sign in to vote.
4.11/5 (2 votes)
17 Nov 2015CPOL1 min read 36.4K   3   1
System overlay permissions in Android M

Introduction

This tip explains some intricacies around system overlay permissions introduced in Android M.

Background

Android has allowed applications to draw over all other applications using the SYSTEM_ALERT_WINDOW permission. This was allowed as long as the permission was declared in the manifest.

XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
...
    </application>
...
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

</manifest>

This technique was used by several applications that required drawing over other apps. A good example is the Facebook chatheads that float over other apps.

What Happened in M?

In Android M, the SYSTEM_ALERT_WINDOW permission was categorized as a 'dangerous' permission. This is understandable because you can imagine a transparent application intercepting your touches or inputs. So, instead of allowing the applications to use the overlay with just the manifest declaration, in Android M, the user needs to provide specific permission in Android settings for the app to draw over other apps.

This setting could be found in Settings -> Apps -> Advanced (settings icon) -> Draw over other apps

Image 1

As you may be aware, Android M did introduce fine grained application permissions that the application can request for. But, as this is classified as a dangerous permission, applications cannot request this permission to the user specifically.

Solution

One possible solution is to direct the user to the above screen if the user is running Android M or above. Fortunately, Android does provide a ready made intent action that you could use to launch the above Activity.

Code:

Java
// Check if Android M or higher
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
    // Show alert dialog to the user saying a separate permission is needed
    // Launch the settings activity if the user prefers
    Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
    startActivity(myIntent);
}

That's it. The action ACTION_MANAGE_OVERLAY_PERMISSION directly launches the 'Draw over other apps' permission screen.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
United States United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.

Comments and Discussions

 
SuggestionMissing Image Pin
Abhishek Pant17-Nov-15 18:10
professionalAbhishek Pant17-Nov-15 18:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.