0

私は音楽プレーヤーアプリケーションを開発しています。ここでは、アプリが最小化(一時停止)されているときに再生/一時停止イベント用の小さな浮動ボタンを画面に表示したいと考えています。 (facebook messenger chat headのような機能性)。それは、SDK < 23を持つデバイスで完璧に正常に動作します。しかし、SDK> = 23のデバイスでは、アプリケーションがインストールされているときにのみ初めてアクセス許可を求めます。ドローオーバーレイ権限は、アプリのインストール時に最初に尋ねられるメッセージです。

許可が与えられていれば、フローティングボタンも完全に表示されます。しかし、いったんアプリケーションを閉じてもう一度起動すると、もうフローティングボタンが表示されません。

許可ダイアログボックスを開くための私のコードは次のとおりです。アプリが一時停止されたとき(ホームボタンや戻るボタンがアプリを最小化するために押されたとき)

public final static int REQUEST_CODE = 100; 

public void checkDrawOverlayPermission() { 
    /** check if we already have permission to draw over other apps */ 
    if (!Settings.canDrawOverlays(this)) { 
     /** 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); 
    } 
} 

@Override 
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)) { 
      // continue here - permission was granted 
      if(isServiceRunning != true){ 
       isServiceRunning = true; 
       intent12 = new Intent(this, notificationService.class); 
       bindService(intent12, notificationConnection, Context.BIND_AUTO_CREATE); 
       startService(intent12); 
      } 
     } 
    } 
} 

私は、フローティングボタンを表示しています。

@Override 
protected void onPause() { 
    super.onPause(); 
    if (Build.VERSION.SDK_INT >= 23) { 
     checkDrawOverlayPermission(); 
    } else { 
     if(isServiceRunning != true){ 
      isServiceRunning = true; 
      intent12 = new Intent(this, notificationService.class); 
      bindService(intent12, notificationConnection, Context.BIND_AUTO_CREATE); 
      startService(intent12); 
     } 
    } 
} 

しかし、私はここに欠けているものを理解することはできません。だから私はこのように、私のアプリmainActivityの onPause()方法でこの checkDrawOverlayPermission()メソッドを呼んでいます。私は、小さなダイアログで許可を求めるとともに、画面上のフローティングボタンを最初に表示するので、許可を確認するためのコードは大丈夫だと思います。助けてください。ありがとう!

答えて

1

既にSDK> 23にアクセス許可が与えられている場合は、何もしません。 checkDrawOverlayPermissionメソッドにelse部分を追加します。

public void checkDrawOverlayPermission() { 
    /** check if we already have permission to draw over other apps */ 
    if (!Settings.canDrawOverlays(this)) { // WHAT IF THIS EVALUATES TO FALSE. 
     /** 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); 
    } else { // ADD THIS. 
     // Add code to bind and start the service directly. 
    } 
} 
+0

ありがとうございました。 –

関連する問題