2010-07-30 72 views
0

我想知道如何在长按媒体按钮时启动一项活动。 在这种情况下,我不想启动默认活动:媒体阅读器,当媒体按钮被短按时,这个媒体阅读器必须保持开启状态。如何处理长按媒体按钮,以启动活动?

希望,我已经明白了。

AL

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

回答

6

我认为你需要做的是这样的:

  1. 添加到您的清单:

    <receiver android:name="com.example.MediaButtonReceiver"> 
        <intent-filter android:priority="1000000000"> 
        <action android:name="android.intent.action.MEDIA_BUTTON" /> 
        </intent-filter> 
        </receiver> 
    
  2. 创建类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(); 
           } 
          } 
         } 
        } 
    } 
    

我主要复制这个从这里:https://android.googlesource.com/platform/packages/apps/Music/+/master/src/com/android/music/MediaButtonIntentReceiver.java

此外,这取决于MEDIA_BUTTON广播正在订购,我认为这是。优先级应设置为高于媒体应用程序。不幸的是,媒体应用程序使用默认优先级,文档没有说明什么是(quelle惊喜)。