Broadcast to read volume buttons

0

I'm having a problem with my broadcast.

I'm not able to do that by pressing any volume button (vol +/-) my Toast is displayed. This is the most important of my manifesto:

    <receiver android:name="Broadcast.b_Botones" >
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
    </receiver>

and this is the simple code of my broadcast:

public class b_Botones extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context,"arranca", Toast.LENGTH_LONG).show();
}}

When you press the volume keys, nothing happens, search and I did not find anything to solve the problem. Yapa, if you know how to capture that button of the two preciono, I would serve too much!

This is what I can recover from the logcat

Thank you very much!

    
asked by LcsGrz 21.09.2018 в 00:17
source

2 answers

0

The answer to my question is simple, but you are going to have to think about a solution for your codes.

I have android 8 on the phone, the problem? Is that from android 7 there were changes in security and restrictions were added, but with android 8 were added even more restrictions, which is why they are not allowed to create certain types of broadcast and that are working permanently, if not they must be registered at the time they wish to use or need, and once the application is finished they must be deleted.

For more information, I'll link you to the Google documentation

BROADCAST BROADCAST 2

    
answered by 03.12.2018 / 21:15
source
0

You can also use this code, this I use to determine the volume.

 public boolean onKeyDown(int keyCode, KeyEvent event) { 
 AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);

 if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)){ 
 Log.i(TAG, "VOLUME UP PRESSED!"); 
 Toast.makeText(context,"arranca", Toast.LENGTH_LONG).show();

 }
 else if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN))
 { 
 Log.i(TAG, "VOLUME DOWN PRESSED!"); 
 Toast.makeText(context,"arranca", Toast.LENGTH_LONG).show();

 } 
 return true; 
 }
    
answered by 21.09.2018 в 07:07