0
如何在用户按下服务或广播接收器中的各种按钮时获得关键事件?我特别想知道用户何时按下音量按钮,以便可以在背景中触发其他内容,如录音机。如何检测服务中的关键事件和广播接收器
不幸的是,我的网络搜索没有产生任何结果。
如何在用户按下服务或广播接收器中的各种按钮时获得关键事件?我特别想知道用户何时按下音量按钮,以便可以在背景中触发其他内容,如录音机。如何检测服务中的关键事件和广播接收器
不幸的是,我的网络搜索没有产生任何结果。
像下面这样的东西应该工作:
从官方文档:接收的
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"...>
//code, like activities, etc
<receiver android:name="com.example.test.VolumeBroadcast" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</application>
例子:
public class VolumeBroadcast extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
//check the intent something like:
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (KeyEvent.KEYCODE_MEDIA_PLAY == event.getKeyCode()) {
// Handle key press.
}
}
}
}
您注册的方法是这样的:
AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);
// Start listening for button presses
am.registerMediaButtonEventReceiver(RemoteControlReceiver);
// Stop listening for button presses
am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
页面下:
Audio Playback
您是否尝试过的东西? –
您需要从小处着手,比如什么是广播接收器,我该如何启动它。接下来分析录音笔样本。 –
当然。我尝试了所有我用Google搜索的东西。但不工作! –