我正在尝试检测(通过 BroadcastReciever)对原始三星耳机按钮的硬按。
代码很简单:
public class MediaButtonIntentReceiver extends BroadcastReceiver {
public MediaButtonIntentReceiver() {
super();
}
@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
Toast.makeText(context, "Action: "+intentAction, Toast.LENGTH_SHORT).show();
if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
return;
}
Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show();
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();
}
显现:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<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 android:priority="100000000000000" >
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
</application>
我已经在galaxy s1(2个有根设备,什么都没有发生)和galaxy NOTE(媒体已启动/停止并且音量已相应更改,但代码中没有发生任何事情..)中对其进行了测试,在HTC中一切正常。我 99% 确定这不是优先级问题,因为我多次更改它。
如果有人知道那里发生了什么,我将不胜感激。谢谢。乌迪