2016-03-08 13 views
1

这是我的清单广播接收器在Android中没有登记

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="21" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <receiver android:name="com.masud.remotecontrol.MyRemoteReceiver" > 
     <intent-filter android:priority="1000000000000000" > 
      <action android:name="android.intent.action.MEDIA_BUTTON" /> 
     </intent-filter> 
    </receiver> 
</application> 

我的接收机

static int n_click = 0; 

@Override 
public void onReceive(final Context context, Intent intent) { 

    String intentAction = intent.getAction(); 

    if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) { 
     return; 
    } 
    KeyEvent event = (KeyEvent) intent 
      .getParcelableExtra(Intent.EXTRA_KEY_EVENT); 
    if (event == null) { 
     return; 
    } 
    int action = event.getAction(); 
    switch (event.getKeyCode()) { 
    case KeyEvent.KEYCODE_HEADSETHOOK: 
     if (action == KeyEvent.ACTION_DOWN) { 
      n_click++; 
      Handler handler = new Handler(); 
      Runnable r = new Runnable() { 

       @Override 
       public void run() { 

        if (n_click == 1) { 

         Log.e("Click==", "Single Click"); 

        } 

        if (n_click == 2) { 

         Log.e("Click==", "Double Click"); 

        } 
        n_click = 0; 
       } 
      }; 
      if (n_click == 1) { 

       handler.postDelayed(r, 500); 
      } 
     } 
     break; 
    } 
    abortBroadcast(); 
} 

我已经试过我的水平最好注册我的接收器,但我有没有得到任何解决方案来注册。我已经尝试了所有在stackoverflow中找到的解决方案,但没有人工作。

回答

0

以上所有代码都可以。我只是将这段代码添加到我的MainActivity中。

((AudioManager) getSystemService(AUDIO_SERVICE)) 
      .registerMediaButtonEventReceiver(new ComponentName(this, 
        MyRemoteReceiver.class));