Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package io.sjf.overlay;
- import android.annotation.TargetApi;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Build;
- import android.os.Bundle;
- import android.provider.Settings;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- public class MainActivity extends AppCompatActivity {
- private static final String TAG = "Overlay";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- if (checkDrawOverlayPermission()) {
- startOverlayService();
- }
- }
- private static int REQUEST_CODE = 1;
- private boolean checkDrawOverlayPermission() {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- /** check if we already have permission to draw over other apps */
- if (!Settings.canDrawOverlays(this)) {
- Log.d(TAG, "canDrawOverlays NOK");
- /** if not construct intent to request permission */
- Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
- Uri.parse("package:" + getPackageName()));
- /** request permission via start activity for result */
- startActivityForResult(intent, REQUEST_CODE);
- return false;
- } else {
- Log.d(TAG, "canDrawOverlays OK");
- }
- }
- return true;
- }
- @Override
- @TargetApi(Build.VERSION_CODES.M)
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- /** check if received result code
- is equal our requested code for draw permission */
- if (requestCode == REQUEST_CODE) {
- if (Settings.canDrawOverlays(this)) {
- startOverlayService();
- }
- }
- }
- private void startOverlayService() {
- Intent intent = new Intent(this, OverlayService.class);
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- startForegroundService(intent);
- } else {
- startService(intent);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement