Vin
Vin

Reputation: 109

My broadcast receiver class not responding for volume button press?

I'm using broadcast receiver on volume button press for creating a shortcut for call. But broadcast receiver not responding for any volume button press and not getting any exception also.

Here is my AndroidManifest.xml class

<receiver android:name="com.abc.utilities.CallBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.media.VOLUME_CHANGED_ACTION" />
        </intent-filter>
</receiver>

My BroadcastReceiver class

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    if(intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")){            
        Lod.d("BroadCast", "Volume button pressed."); 
    }
}

Upvotes: 2

Views: 4542

Answers (1)

lopez.mikhael
lopez.mikhael

Reputation: 10061

I try to code what you want and it's work.

1) I defined my BroacastReceiver in manifest like that :

...
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="com.example.test.CallBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.media.VOLUME_CHANGED_ACTION" />
        </intent-filter>
    </receiver>
</application>
...

2) I created CallBroadcastReceiver :

package com.example.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class CallBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        int volume = (Integer)intent.getExtras().get("android.media.EXTRA_VOLUME_STREAM_VALUE");
        Log.i("Tag", "Action : "+ intent.getAction() + " / volume : "+volume);      
    }

}

You don't need to check the value of the Action of your intent because your BroadcastReceiver just listened VOLUME_CHANGED_ACTION. But if you checked the value of your action it's android.media.VOLUME_CHANGED_ACTION.

After, I try this app in my Nexus 6, it's possible that the problem comes from the phone.

Upvotes: 3

Related Questions