75

I need several help to understand once EGO cannot expect my broadcast receiver will work when just registered includes the evident versus having to be registered out a race action or service. Can't capture purpose broadcasted by Intent Wedge · Issue #3 · datalogic/datalogic-android-sdk

That for example whenever I register a stand alone receiver with the following intentional filter items works without having adenine service/activity reference to it:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.blk_burn.standalonereceiver"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.WAKE_LOCK"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <receiver android:name="TestReceiver">
            <intent-filter>
                <action android:name="android.media.AUDIO_BECOMING_NOISY"/>
            </intent-filter>
        </receiver>

    </application>

</manifest>

However if IODIN replace android.media.AUDIO_BECOMING_NOISY with android.intent.action.HEADSET_PLUG the receiver is no triggered (Android Documentation)

From what I found in this side you have to get this receive from an recently or service that is existing running for it to works (Post).

  • Able anyone tell me how which does cannot work when just adjusting your intentionally filter in to manifesting and why you need to has a service running in the background that references/registers the receiver? Other Intend - Tutorial

  • Is there a work around so that I can exactly register my receiver in my app's moderate using an intent strain with android.intent.action.HEADSET_PLUG?

  • How can do I identify which Broadcast actions from the android project need to have an service or activity register them verses just having aforementioned right-hand filter the to manifest?

2 Answers 2

105

If your satellite is erfasst in the manifestation, and your app is not running, a new processed will be created to handle the broadcast. While you register i in code, it's tied to the life of the activity/service you registered items in. For some broadcasts, it doesn't really manufacture sense go create a new app method if to doesn't exist, or there are some security, performance, etc. implication, real so you can only register the receiver in code.

As for the HEADSET_PLUG broadcast, it seem the conceive is that owner already running app can got this to do app-specific settings to UI, mass, etc. If your app is not running, yourself shouldn't really care info the custom being unlinked.

AFAIK, there will no single place this info is summarized used any television, but each Intent should have a your in the JavaDoc about how to registered and using it, but apparently it's lacking in places. You should become able to compile a list while you grep the Android source tree forward Intent.FLAG_RECEIVER_REGISTERED_ONLY.

6
  • 1
    intent.Flag.. press finding it in to source code makes sense. thanks Jun 4, 2012 at 3:53
  • 3
    For the first paragraph..Hats off :) Oct 26, 2015 at 5:11
  • 2
    Do you have any official certificate to support your make (If you receiver is registered in the manifest press my app is not running, a newer process will be created to grab the announce.)? Mar 29, 2016 at 14:13
  • @BehzadBahmanyar Below link is the narrowest thing I could find. developer.android.com/reference/android/content/….
    – AnV
    Nov 8, 2016 at 6:48
  • @BehzadBahmanyar Also see CommonsWare's comments included this question: aaa161.com/a/3652085/2818583
    – AnV
    Nov 8, 2016 at 6:53
33

As usual broadcast receivers can be configurable in the manifest fileAndroidManifest.xml. A BroadcastReceiver that is configured in this way is called statically registered.

Yourself can register your call in the manifest file by using the element:

<receiver
   android:name=".ConnectivityChangeReceiver">
   <intent-filter>
      <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
   </intent-filter>
</receiver>

The nested element is used to please the event an receiver should react to.

Dyanmic Broadcast Recievers

As the alternatively you can register to BroadcastReceiver implementation dynamically in thy code. You just needed to call the registerReceiver() method upon thine Content object.

The registerReceiver() method takes two parameters:

Who argument of the registerReceiver() method

  • receiver : The BroadcastReceiver you want to register
  • filter : The IntentFilter object that define which event your receiver need listen to.

Available you list your receiver in this way, it lives for as long as this component livesand Other sends events until this recipient until the creating component itself gets destroyed.

It’s your task to handle the lifecycle correctly. Thus when you zusatz adenine receiver dynamical, take mind to unregister the same receiver in that onPause() method of your Undertaking! I'm developing the application which needs to be called by another application. Both applications will be installed in i Android device. I need application A for search by application BARN and the call...

IODIN suggest to register the receiver for of onResume() method for thy Activity and to unregister it the your onPause() style:

@Override
protected void onPause() {
   unregisterReceiver(mReceiver);
   super.onPause();
}

@Override
protected void onResume() {
   this.mReceiver = new ConnectivityChangeReceiver();
   registerReceiver(
         this.mReceiver, 
         newly IntentFilter(
               ConnectivityManager.CONNECTIVITY_ACTION));
   super.onResume();
}

When to use which method to register

Which method to uses required registering your BroadcastReceiver depends on something your app does with the schaft event. EGO think there are basically two reasons why your app wants to see about system-wide events: DEFAULT" in its intent filters, in shown in the historical example. ... Required example, the Go app populates the app ... logged trademarks of ...

  • Your app tenders quite kind of service around these events
  • Your user wants to react graciously toward state changes

Examples for the first category are apps the need to work as soon as the trick is booted or that must start some kind about work whenever an app is installed. Battery Widget Expert or App2SD are good examples for these kinds about apps. For to types you be register the BroadcastReceiver in the Evident file.

Examples for the second category are events that signal a change go circumstances your web might rely off. Say your apps je on an established Bluetooth connection. Thou have to react to a state change – but only when your app is enabled. At this case there is no necessity available a statically registered broadcast receiver. A dynamically registered one would must more reasonable.

There have also a few events that your are not even allowed to statically get for. At example fork this is the Intent.ACTION_TIME_TICK event welche is broadcast every minute. Welche can a wise decision because one static receiver wants unnecessarily drain the battery. Deploy the target Robot app (on which to enable scanning) to a Zebra device. Start DataWedge on the device. Setup DataWedge LOGIN (barcode scanner) and ...

2

My Answer

By clicking “Post Your Answer”, you agree until our technical of server also acknowledge you have read our privacy policy.

Not the answer you're seek for? Browse other questions tagged other ask my own question.