title: how to disable the question (Do you want to ignore battery optimization?) android studio
body: <p>I was having problem whit android media player because when I turn off the screen the system goes to sleep mode and stop it and I didnt want that because nobody wants to listen music and have the screen on (who do that?). </p>
  
  <p>I noticed that the problem was on the battery optimizations because if the mobile was plug to power cable it keeps the music playin even if it goes to sleep mode. </p>
  
  <p>I tried with a lot of posibilities to fix it (reading android documentation and trying every thing I was finding in the net) but what works was this code:</p>
  
  <pre><code>public void doPower() {
      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
          String packageName = getActivity().getPackageName();
          PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
          if (!pm.isIgnoringBatteryOptimizations(packageName)) {
              try {
                  //some device doesn't has activity to handle this intent
                  //so add try catch
                  Intent intent = new Intent();
                  intent.setAction(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                  intent.setData(Uri.parse("package:" + packageName));
                  startActivity(intent);
              } catch (Exception e) {
              }
          }
      }
  } 
  </code></pre>
  
  <p>The problem here is: The app at the beginning shows an alert asking the user to disable the battery optimizations and I dont want that, because some users may thinks "this is a bad thing" or "why its asking this?" Because an app like spotify doesnt do it. </p>
  
  <p>How can I avoid the alert message?  If I cant, how can I add something else to the message? (something like this: "So you will able to listen the radio with the screen turn off")</p>
  
  <p>Thanks in advance and best regards. </p>