Last active
May 15, 2017 06:22
-
-
Save Bhavdip/7d206cd6b724260f32a1194221ab0ec1 to your computer and use it in GitHub Desktop.
[On Top Of Other Application] #tags:Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ChatHeadService extends Service { | |
private WindowManager windowManager; | |
private ImageView chatHead; | |
@Override public IBinder onBind(Intent intent) { | |
// Not used | |
return null; | |
} | |
@Override public void onCreate() { | |
super.onCreate(); | |
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); | |
chatHead = new ImageView(this); | |
chatHead.setImageResource(R.drawable.android_head); | |
WindowManager.LayoutParams params = new WindowManager.LayoutParams( | |
WindowManager.LayoutParams.WRAP_CONTENT, | |
WindowManager.LayoutParams.WRAP_CONTENT, | |
WindowManager.LayoutParams.TYPE_PHONE, | |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, | |
PixelFormat.TRANSLUCENT); | |
params.gravity = Gravity.TOP | Gravity.LEFT; | |
params.x = 0; | |
params.y = 100; | |
windowManager.addView(chatHead, params); | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
if (chatHead != null) windowManager.removeView(chatHead); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> It is a new behaviour introduced in Marshmallow 6.0.1 | |
> Every app that requests the SYSTEM_ALERT_WINDOW permission and that is installed through the Play Store (version 6.0.5 or higher is required), will have granted the permission automatically. | |
> If instead the app is sideloaded, the permission is not automatically granted. | |
Screen-overlay-detected-blocks-android-permissions | |
======================================================\ | |
This popup is caused by the manifest.PERMISSION.SYSTEM_ALERT_WINDOW permission declared by the manifest. The are 3 categories of permissions, that developer must be aware of.: | |
A. Normal permission - do nothing with them, just declare in the Manifest | |
B. Vulnerable permissions - declare in Manifest and ask for permission at first time. They can be changed through system settings | |
C. Above dangerous permissions: SYSTEM_ALERT_WINDOW and WRITE_SETTINGS belong to this category. They must be granted, but are not visible in system settings. To request for it you don't use a standard way (int checkSelfPermission (String permission)) but you have to check Settings.canDrawOverlays() or Settings.System.canWrite() appropriately | |
You will need <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> | |
public class MainActivity extends AppCompatActivity { | |
public final static int REQUEST_CODE = 10101; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
if (checkDrawOverlayPermission()) { | |
startService(new Intent(this, PowerButtonService.class)); | |
} | |
} | |
public boolean checkDrawOverlayPermission() { | |
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { | |
return true; | |
} | |
if (!Settings.canDrawOverlays(this)) { | |
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, | |
Uri.parse("package:" + getPackageName())); | |
startActivityForResult(intent, REQUEST_CODE); | |
return false; | |
} else { | |
return true; | |
} | |
} | |
@Override | |
@TargetApi(Build.VERSION_CODES.M) | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (requestCode == REQUEST_CODE) { | |
if (Settings.canDrawOverlays(this)) { | |
startService(new Intent(this, PowerButtonService.class)); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment