简体   繁体   English

广播接收器未在Android中注册

[英]Broadcast receiver not registering in Android

This is my manifest 这是我的表现

<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>

My receiver 我的接收者

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();
}

I have tried my level best to register my receiver but I have not get any solution in to register. 我已经尽我最大的努力来注册接收者,但是我还没有获得任何解决方案来注册。 I have tried all the solution found in stackoverflow but no one is working. 我已经尝试了在stackoverflow中找到的所有解决方案,但是没有人在工作。

All the above code is ok. 以上所有代码都可以。 I just add this code to my MainActivity. 我只是将此代码添加到我的MainActivity中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM