按耳机按钮并触发事件?

时间:2017-06-22 18:37:39

标签: android

我有耳机,耳机。它在垂直方向上有按钮,增加音量,中间按钮,音量较低。

按下中间按钮,即使屏幕关闭或手机锁定,它也会打开Spotify并播放音乐。

我想为我的应用程序执行此操作,当按下此中间按钮时,此应用程序触发事件,是否可能?或者,按其中一个音量按钮并触发事件。

有可能吗?如果是,是否有任何示例,源代码,与此应用程序?

编辑:

代码不起作用:

主:

public class MediaButtonIntentReceiver extends BroadcastReceiver {
    public MediaButtonIntentReceiver() {
        super();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String intentAction = intent.getAction();
        if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
            return;
        }
        KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return;
        }
        int action = event.getAction();
        if (action == KeyEvent.ACTION_DOWN) {
            toDo();
            Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show();
        }
        abortBroadcast();
    }

广播:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.aula.bob.headset">

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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

    </application>

</manifest>

清单:

IEnumerable

1 个答案:

答案 0 :(得分:0)

在您的应用中创建一个扩展广播接收器的类:

public class MediaButtonIntentReceiver extends BroadcastReceiver {

public MediaButtonIntentReceiver() {
super();
}

@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
    return;
}
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
    return;
}
int action = event.getAction();
if (action == KeyEvent.ACTION_DOWN) {
// do something
    Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show(); 
}
abortBroadcast();
}
}

在您的活动类中:

public class MainActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

 IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);//"android.intent.action.MEDIA_BUTTON"
MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();
filter.setPriority(10000); //this line sets receiver priority
registerReceiver(r, filter);

}
}

在Android Manifest中定义您的Receiver,如下所示:

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

希望它对你有用!