1

I want to be alerted, if a headset is plugged in. But the Intent "ACTION_HEADSET_PLUG" is used with the Flag "FLAG_RECEIVER_REGISTERED_ONLY". So I have to run a service or a activity all the time to get the intent? I just want to run a Broadcastreceiver and NO service or activity at all. Is there a workaround for this? Is this even possible? Or can I start a service once, registering and then stopping it?

P.S.: I know when the headset is plugged off with the becoming noisy intent.

Thank you for you answers.

keiki
  • 3,260
  • 3
  • 30
  • 38

2 Answers2

4

Dianne Hackborn says

It's only given to those who call registerReceiver, and not sent to receivers declared in manifests.

So you have to have something running that calls registerReceiver. It that stops running and you don't unregister the receiver you get an error.

zapl
  • 63,179
  • 10
  • 123
  • 154
0

There are components that should be registered when the application is installed (<3.0), or at least when the application has moved from stopped state to started (3.0 & ICS), such as a ContentProvider.

The provider apparently doesn't have an "end" to it's life cycle that you have to worry (a lot) about.

Simply put, adding a <provider> to your AndroidManifest.xml:

<provider android:name=".ReceiverProvider"
    android:authorities="some.provider.authority.goes.here"
    android:exported="true" />

And, for the ReceiverProvider:

public class ReceiverProvider extends ContentProvider {
    @Override
    public boolean onCreate() {
        BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                System.out.println("Headset plug: " + intent);
            }
        };
        IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
        getContext().registerReceiver(receiver, filter);
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }
}

Worth trying perhaps.

Jens
  • 16,853
  • 4
  • 55
  • 52
  • here you have the case of "not sent to receivers declared in manifests" – zapl Mar 13 '12 at 18:36
  • That's grossly **incorrect**. The receiver is not declared in a manifest, it's part of a :s onCreate - which is triggered when an application goes from stopped to started state (or immediately, if sdk_version <= 10). – Jens Mar 14 '12 at 07:46
  • oops, true. But afaik a ContentProvider does not get started until someone requests content from you. I guess you could catch `Intent.ACTION_BOOT_COMPLETED` and then call `context.getContentResolver().acquireContentProviderClient(Uri.parse("content://your.authority"))` to have that happen. – zapl Mar 14 '12 at 10:01
  • ContentProviders are started on the application main thread when the application is started. You do not need to request anything from it. – Jens Mar 14 '12 at 10:08
  • Yes, but I guess OP does not want users to have to start the app manually each time – zapl Mar 14 '12 at 10:37