Android Example Intents and receivers

With the new imodules that allow you to work better with the intent and receivers, it is necessary to develop some examples of how to use them (even before it could be done but not in such a direct way).

SOME DETAILS
This B4A update handles static receivers not dynamic ones. Some intents no longer raise static receivers such as CONNECTIVITY_CHANGE which due to the restrictions applied by Android 7+ need to be registered with dynamic receivers by code (see here for dynamics receivers)
Receivers can be raised by the OS listening to the intent or directly by an App calling the intent directly


SAMPLE: LISTEN USB PORT
I start with an example that through the use intent it is possible to 'hear' when a usb device is connected. In this case it is the operating system that manages the intents. Remember to place the filter XML file in the object/res/xml folder as it is in the example (You can customize the filter if you want)
In this example, you can scan the USB port and see what devices are connected.
Or wait for each time a device is connected to call the intent that updates the list and plays a warning sound together with a ToastMessage
 

Attachments

  • device_filter.zip
    413 bytes · Views: 374
  • IntentUSB.zip
    10.6 KB · Views: 369
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
START AT BOOT
This example allows you to recall the receiver after the device has completely booted up (usually after a few minutes)
Clearly the receiver will not be able to start an activity if it is not in the foreground or an App with DrawOnApp permissions
 

Attachments

  • BOOTstart.zip
    9.7 KB · Views: 297
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
MORE Apps that communicate through intents.
This is the example I like best. They are two or more Apps that communicate through intents.
The first App offers services or data that are requested by other apps through an intent. We will call this App provider.

The other Apps send a request (complete with any parameters or data) through the broadcast intent to the App provider and receive the response. We will call this App Requester
 

Attachments

  • Provider.zip
    9.6 KB · Views: 323
  • Requester.zip
    9.2 KB · Views: 348
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
Now that the services are killed it has been suggested to use StartReceiverAt rather than StartServiceAtExact.

Here is an example of how to use it
 

Attachments

  • StartReceiverAt.zip
    9.9 KB · Views: 338
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
LISTENING TO SMS
In this example, the handset is lifted when the device receives an SMS
 

Attachments

  • SMSreceived.zip
    10.1 KB · Views: 297
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
Reserved
 

Star-Dust

Expert
Licensed User
Longtime User
reserved
 
Top