如何处理长preSS媒体按钮,以推出活动? [英] How to handle long press media button, in order to launch activity?

查看:104
本文介绍了如何处理长preSS媒体按钮,以推出活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何启动一个活动时长pressing媒体按钮。 在这种情况下,我不希望启动默认的活动:媒体的读者,这其中必须保持空空时,媒体键就少了pressed

I would want to know how to launch an activity when long pressing media button. In this case, I don't want to launch the default activity : the media reader, this one must keep lauching when media button has been short pressed.

希望,我一直explicite。

Hope, I've been explicite.

A.L。

附属的问题:为什么有些硬键,如搜索按钮,可以直接启动活动在manifest.xml,和其他人一样,媒体按钮的活动属性指定它,只提到了广播行动

Subsidiary question : Why some hard key, like the search button, can directly launch activity specifying it in the activity attribute of the manifest.xml , and others, like media button, are only mentioned for broadcast action ?

推荐答案

我想你需要做的是这样的:

I think what you need to do is this:

  1. 添加到您的清单:

  1. Add this to your manifest:

<receiver android:name="com.example.MediaButtonReceiver">
   <intent-filter android:priority="1000000000">
    <action android:name="android.intent.action.MEDIA_BUTTON" />
   </intent-filter>
    </receiver>

  • 创建类 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();
                    }
                }
            }
        }
    }
    

  • 我大多复制此从这里开始:<一href="http://android.git.kernel.org/?p=platform/packages/apps/Music.git;a=blob_plain;f=src/com/android/music/MediaButtonIntentReceiver.java;h=6b5a9d8f8769c7ea14cac58521729f98f109a189;hb=0dc3115358faa5573b68703469b5799cdd829499">http://android.git.kernel.org/?p=platform/packages/apps/Music.git;a=blob_plain;f=src/com/android/music/MediaButtonIntentReceiver.java;h=6b5a9d8f8769c7ea14cac58521729f98f109a189;hb=0dc3115358faa5573b68703469b5799cdd829499

    I mostly copied this from here: http://android.git.kernel.org/?p=platform/packages/apps/Music.git;a=blob_plain;f=src/com/android/music/MediaButtonIntentReceiver.java;h=6b5a9d8f8769c7ea14cac58521729f98f109a189;hb=0dc3115358faa5573b68703469b5799cdd829499

    此外,这依赖于 MEDIA_BUTTON 广播被责令,我认为它是。应该优先设置不是媒体应用程序更高。不幸的是,媒体应用程序使用默认的优先级,并且该文档不说那是什么(广利惊喜)。

    Also, this depends on the MEDIA_BUTTON broadcast being ordered, which I assume it is. The priority should be set to something higher than the media app. Unfortunately the media app uses the default priority, and the documentation doesn't say what that is (quelle surprise).

    这篇关于如何处理长preSS媒体按钮,以推出活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆