non*_*zor 0 media android button launch android-activity
我想知道如何在长按媒体按钮时启动活动.在这种情况下,我不想启动默认活动:媒体阅读器,当短按媒体按钮时,必须保持该功能.
希望,我已经明白了.
AL
辅助问题:为什么某些硬键(如搜索按钮)可以直接启动在manifest.xml的activity属性中指定它的活动,而其他硬件(如媒体按钮)仅用于广播操作?
我想你需要做的是:
将此添加到您的清单:
<receiver android:name="com.example.MediaButtonReceiver">
<intent-filter android:priority="1000000000">
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)创建类MediaButtonReceiver:
public class MediaButtonReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
KeyEvent event = (KeyEvent)
intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
return;
}
int keycode = event.getKeyCode();
int action = event.getAction();
long eventtime = event.getEventTime();
if (keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE || keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
if (action == KeyEvent.ACTION_DOWN) {
// Start your app here!
// ...
if (isOrderedBroadcast()) {
abortBroadcast();
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)我大多从这里复制了这个:https://android.googlesource.com/platform/packages/apps/Music/+/master/src/com/android/music/MediaButtonIntentReceiver.java
此外,这取决于MEDIA_BUTTON正在订购的广播,我认为这是.优先级应设置为高于媒体应用程序的值.不幸的是,媒体应用程序使用默认优先级,文档没有说明那是什么(quelle surprise).
| 归档时间: |
|
| 查看次数: |
4799 次 |
| 最近记录: |