简体   繁体   English

通过我的Android应用访问音量按钮

[英]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". 我正在开发一个Android应用程序,该应用程序应该能够识别何时有人按下了手机的硬件音量调高按钮,并发出通知说“音量调高按钮已按下”。

This is my BroadcastReceiver class. 这是我的BroadcastReceiver类。

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. 由于我是android开发的新手,所以我不知道如何实现MainActivity.java文件。

This is the MainActivity.java file I have wrote so far. 这是我到目前为止编写的MainActivity.java文件。 But it gives an error saying "The method registerMediaButtonEventReceiver(HardwareButtonReceiver) is undefined for the type MainActivity" 但是它给出一个错误,指出“对于MainActivity类型,方法registerMediaButtonEventReceiver(HardwareButtonReceiver)未定义”

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. 如果你们能帮助我解决我的问题,我会非常感激。

You must add method registerMediaButtonEventReceiver to your activity class. 您必须将方法registerMediaButtonEventReceiver添加到您的活动类中。

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. 然后实现它。

http://developer.android.com/reference/android/view/KeyEvent.html 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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM