Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. package io.sjf.overlay;
  2.  
  3. import android.annotation.TargetApi;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.os.Build;
  7. import android.os.Bundle;
  8. import android.provider.Settings;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.support.v7.widget.Toolbar;
  11. import android.util.Log;
  12. import android.view.Menu;
  13. import android.view.MenuItem;
  14.  
  15. public class MainActivity extends AppCompatActivity {
  16.  
  17. private static final String TAG = "Overlay";
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. if (checkDrawOverlayPermission()) {
  24. startOverlayService();
  25. }
  26. }
  27.  
  28. private static int REQUEST_CODE = 1;
  29. private boolean checkDrawOverlayPermission() {
  30. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  31. /** check if we already have permission to draw over other apps */
  32. if (!Settings.canDrawOverlays(this)) {
  33. Log.d(TAG, "canDrawOverlays NOK");
  34. /** if not construct intent to request permission */
  35. Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
  36. Uri.parse("package:" + getPackageName()));
  37. /** request permission via start activity for result */
  38. startActivityForResult(intent, REQUEST_CODE);
  39. return false;
  40. } else {
  41. Log.d(TAG, "canDrawOverlays OK");
  42. }
  43. }
  44. return true;
  45. }
  46.  
  47. @Override
  48. @TargetApi(Build.VERSION_CODES.M)
  49. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  50. /** check if received result code
  51. is equal our requested code for draw permission */
  52. if (requestCode == REQUEST_CODE) {
  53. if (Settings.canDrawOverlays(this)) {
  54. startOverlayService();
  55. }
  56. }
  57. }
  58.  
  59. private void startOverlayService() {
  60. Intent intent = new Intent(this, OverlayService.class);
  61. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  62. startForegroundService(intent);
  63. } else {
  64. startService(intent);
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement