Hasitha Thamaranga
Hasitha Thamaranga

Reputation: 39

Access the volume button through my android app

I am developing an android app which should be able to identify when someone press the hardware volume up button of the phone , and give a notification saying "volume up button has pressed".

This is my BroadcastReceiver class.

package com.example.volumebut;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.widget.Toast;

public class HardwareButtonReceiver extends BroadcastReceiver {



@Override
public void onReceive(Context context, Intent intent) {
 KeyEvent e = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); 
     if(e.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
        Toast.makeText(context, "volume up  button pressed." ,Toast.LENGTH_LONG).show();
     }

}





}

This is my manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.volumebut"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.volumebut.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=".HardwareButtonReceiver">
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
    </receiver>
</application>

</manifest>

Since I am new to android development I have no idea how to implement MainActivity.java file.

This is the MainActivity.java file I have wrote so far. But it gives an error saying "The method registerMediaButtonEventReceiver(HardwareButtonReceiver) is undefined for the type MainActivity"

package com.example.volumebut;

import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;

public class MainActivity extends Activity {


public void onCreate(Bundle savedInstanceState) {

    HardwareButtonReceiver receiver = new HardwareButtonReceiver();

    registerMediaButtonEventReceiver(receiver);

}




}

If you guys can help me to solve my problem I appreciate It much.

Upvotes: 0

Views: 1784

Answers (2)

eleven
eleven

Reputation: 6847

You must add method registerMediaButtonEventReceiver to your activity class.

public class MainActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xml_of_your_activity);
        HardwareButtonReceiver receiver = new HardwareButtonReceiver();
        registerMediaButtonEventReceiver(receiver);
    }

    private void registerMediaButtonEventReceiver(HardwareButtonReceiver receiver) {

    }
}

And then implement it of course.

Upvotes: 0

rajahsekar
rajahsekar

Reputation: 914

http://developer.android.com/reference/android/view/KeyEvent.html

go through above link you will get different key events

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {


            if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
//          
            }
            else if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
            {

            }

        return super.onKeyDown(keyCode, event);
    }

Upvotes: 2

Related Questions