2016-03-08 12 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));