Doubts about Intents and BroadcastReceiver - android

Im quite new on Android programming, so I have some basic doubts.
There is an App here that do not have the BroadcastReceiver, but I used other class instead, and I am sure that it works properly.
I read in some topics, that i need to register It in manifest, but I have no clue how to do It; and I got confused about com.google.android.c2dm.permission.SEND and etc, I do not know how to set it.
Question: Can I ask for someone explain to me, in simple way, what I need to do to my method be executed while the app is closed, AND, how i register it on manifest?
Thanks!

Send an Intent is the way of Android of telling to everyone that some event occured.
For instance where your device receive a call an Intent is broadcast. But to be specific to some event every Intent has an action. For instance the Intent broascast when you receive a SMS has the "android.provider.Telephony.SMS_RECEIVED" action.
In your AndroidManifest.xml you can register objects for specific intents. You can register Activity, Service and BroadcastReceiver.
To register a BroadcastReceiver to "receive sms action" you do the following in your manifest :
<receiver android:name="your.receiver.class">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
This means that every time an Intent with the specified action is fired, it will be pass to your receiver. This means that his onReceive method will be called with the intent as param.
So to create some code that will be executed will your app is closed, follow this steps :
Create a class that extends BroadcastReceiver.
Put your code in the onReceive method. This method will be call every time your receiver receive an intent.
Register your receiver for the desired action in your AndroidManifest.xml file.

BroadcastTeceiver as the name imply is component that could receive data that someone send via Intents. The sender could be the system, other App or your App itself.
There are to ways to regiester BroadcastReceiver:
In the manifest by exlixit the Intent you want to listen.
In code by give it Intent_filter programmatically.

Related

Android - Is it possible to declare Local Broadcast receiver in the manifest file?

I have declared my receiver in my manifest:
<receiver
android:name=".MyTestReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.ACTION_TEST"/>
</intent-filter>
</receiver>
And here is my MyTestReceiver class:
public class MyTestReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ("com.example.ACTION_TEST".equals(action)) {
Toast.makeText(context, "Test!", Toast.LENGTH_SHORT).show();
}
}
}
But when I execute this code from elsewhere within my app:
Intent intent = new Intent("com.example.ACTION_TEST");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
...the local broadcast is not received (i.e., the toast is not shown).
Questions:
Is it possible to register a local broadcast receiver in the manifest?
If so, have I declared my local broadcast receiver incorrectly?
If it's not possible to declare a local broadcast receiver in the manifest, and I declare it in my Application subclass instead, will it have the same 'scope' as a receiver declared in the manifest? (I mean, will it receive broadcasts in all the same conditions/situations as it would if it was declared in the manifest?)
If there is a difference between specifying the receiver in the manifest than in my Application subclass, would I need to use general (non-local) broadcasts rather than local broadcasts? (In the actual app, the local broadcast will be sent when my IntentService completes its work. The IntentService will be triggered by an FCM push message.)
NB - All I can seem to find about this in the documentation is:
Note: To register for local broadcasts, call
LocalBroadcastManager.registerReceiver(BroadcastReceiver,
IntentFilter) instead.
...which doesn't address the main issue of whether or not you can specify the receiver in the manifest.
Is it possible to register a local broadcast receiver in the manifest?
No.
and I declare it in my Application subclass instead, will it have the same 'scope' as a receiver declared in the manifest? (I mean, will it receive broadcasts in all the same conditions/situations as it would if it was declared in the manifest?)
Well, no. A manifest-registered receiver is for system broadcasts, originating from any process. LocalBroadcastManager is local, for "broadcasts" within your own process.
You are welcome to register a receiver with LocalBroadcastManager in your Application (e.g., in its onCreate()), but I suspect that there are better solutions for whatever problem you are trying to solve.
In the actual app, the local broadcast will be sent when my IntentService completes its work
Then the receiver should be registered in the activity or fragment that needs to know about that work being completed. Your Application is unlikely to need to know about that work being completed, as if it did, your IntentService could just call a method on the Application and bypass all this broadcast stuff.

What is the difference between intent filter in activity and broadcast receiver?

Can anybody tell me when should I use intent filter and broadcast receiver?
<activity>
<intent-filter></intent-filter>
</activity>
and
<receiver>
<intent-filter></intent-filter>
</receiver>
I think you are confused about implicit intent and broadcast receiver. Intent Filter in Activity is for receiving implicit intent, while that in Receiver is for receiving broadcast. The OS deliver an broadcast msg to all Receivers, while it deliver an implicit intent to a certain one activity. See here
From the documentation:
A broadcast receiver is a component that responds to system-wide
broadcast announcements. Many broadcasts originate from the system—for
example, a broadcast announcing that the screen has turned off, the
battery is low, or a picture was captured. Applications can also
initiate broadcasts—for example, to let other applications know that
some data has been downloaded to the device and is available for them
to use. Although broadcast receivers don't display a user interface,
they may create a status bar notification to alert the user when a
broadcast event occurs. More commonly, though, a broadcast receiver is
just a "gateway" to other components and is intended to do a very
minimal amount of work. For instance, it might initiate a service to
perform some work based on the event.
You can use broadcast receiver by two ways.
1) Registering and un-registering in your activity.When you register with your activity you need to pass an action for which it will take care and it will fire when we send broadcast with that action from our application.
2) Second way to use broadcast reciver to register in manifest file and mention action in intent filter for that in manifest file.
Intent filter is nothing but in simple words "It is filter just we use in our usual lives." It will filter the actions for calling it.
Intent filter is same for activity and broadcast receiver.Its main functioning is to filtering the action.It depends on us how to utilize it.One major example is in our each application in manifest file we specify
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
in our launcher activity.It suggests this activity is our launcher activity and will run first at start of our application.If you not specify it then your application will not launch.Also we can not specify these types of filter in broadcast receiver's intent filter.They are not launcher of apps,

Boot completed Broadcast receiver is not working for dynamic registration

I created a broadcast receiver and registered in in manifest using following approach it is working fine
static way registering broadcast receiver (working fine)
<receiver
android:name="DeviceRestartListener"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</category> -->
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
But when i tried to register the broadcast receiver programmatic (instead of static way) using following way it doesnt work
Programmetic registering (not working)
DeviceRestartListener dataBroadcastReceiver = new DeviceRestartListener();
IntentFilter filter = new IntentFilter();
filter.addAction(
"android.intent.action.BOOT_COMPLETED")
//registerReceiver(dataBroadcastReceiver, filter); //DOESNT WORK
registerReceiver(dataBroadcastReceiver, filter, "android.permission.RECEIVE_BOOT_COMPLETED", null); //DOESNT WORK
No compilation and run time error. But the receiver is not receiving broadcast after device restarts
Thanks
All the Broadcast receivers will not work even when they are statically declared in manifest or registered dynamically using Application context. for example Intent actions like
Intent.ACTION_SCREEN_OFF
and
Intent.ACTION_SCREEN_ON
have to be registered dynamically. These actions will not be fired when they are declared in manifest. some Intent actions like
Intent.ACTION_TIME_CHANGED;
Intent.ACTION_TIME_TICK;
Intent.ACTION_TIMEZONE_CHANGED;
will be fired when are registered dynamically through context whose window token is not null.(like Activity or Dialog).
similar to this, some of the Intent actions like
Intent.ACTION_BOOT_COMPLETED
will work only when they are registered statically using manifest
Register receiver in code
When we register a receiver in code, we must unregister it when the app gets destroy (actually, when the Activity or Service that register it, gets destroy).
Register receiver in manifest
When we declare it in the manifest, you make it available even if you app is not running.
When to use which method to register
Which method to use for registering your BroadcastReceiver depends on what your app does with the system event. I think there are basically two reasons why your app wants to know about system-wide events:
Your app offers some kind of service around these events
Your app wants to react graciously to state changes
Examples for the first category are apps that need to work as soon as the device is booted or that must start some kind of work whenever an app is installed. Battery Widget Pro or App2SD are good examples for these kinds of apps. For this type you must register the BroadcastReceiver in the Manifest file.
Examples for the second category are events that signal a change to circumstances your app might rely on. Say your app depends on an established Bluetooth connection. You have to react to a state change – but only when your app is active. In this case there is no need for a statically registered broadcast receiver. A dynamically registered one would be more reasonable.
There are also a few events that you are not even allowed to statically register for. An example for this is the Intent.ACTION_TIME_TICK event which is broadcast every minute. Which is a wise decision because a static receiver would unnecessarily drain the battery.

How to start service using external intent?

I'd like to know if it's possible to start service using intents sent from another app? I've a Broadcast Receiver for android.intent.action.BOOT_COMPLETEDand it works even though at the time when the intent is received BrodcastReceiver class for it is not instancionated. I did something similar for external intents from tasker but it doesn't work.
<receiver android:name="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I'd like to know if it's possible to start service using intents sent from another app?
Sure.
I've a Broadcast Receiver for android.intent.action.BOOT_COMPLETEDand it works even though at the time when the intent is received BrodcastReceiver class for it is not instancionated.
That is because manifest-registered BroadcastReceiver objects are not instantiated ahead of time. They are only created when a matching broadcast is sent.
I did something similar for external intents from tasker but it doesn't work.
"it doesn't work" is insufficient information for anyone to help you.
But, if you have a <service> with an <intent-filter>, other apps can create an Intent matching your <intent-filter> and use that to start (or bind to) your service. There are two exceptions:
If you add android:exported="false" to the <service>, third party apps cannot invoke it at all, though you would be better served simply getting rid of the <intent-filter> in that case
If you use android:permission on the <service> element, the other app needs to hold your stated permission in order to start or bind to your service

how do I set a broadcast reciever for getting pending intents (proximate alerts)

i have used broadcast reciever for getting alerts on specific location using pending intents(proximate alerts)
but it do not fire any notification when app is closed or in background how i can do this please help me to set this .
here what i tried in xml:
<receiver android:name=".ProximityIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
i also register it in activity :
IntentFilter filter = new IntentFilter(PROXIMTY_ALERT_INTENT);
registerReceiver(new ProximityIntentReceiver(), filter);
in this way im getting notification when activity is running but afert i closed it i do not get any notification. what is proper way to do this.
You could add more actions to your intent filter, this question here should help you with that:
BroadcastReceiver for location
Or there may be a way of creating a Listener to listen for changes in GPS location. Check here for information: http://developer.android.com/reference/android/location/LocationListener.html
I haven't found any documentation regarding PROXIMTY_ALERT_INTENT. Some broadcasts can't be registered from manifest.xml. If this is one of them you can receive those updates only during the application lifetime.
The receiver you defined for BOOT_COMPLETED needs the following permission to work:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
You can start a service in the receiver for BOOT_COMPLETED intent and register there for the proximity alert. Usually having a service running all the time is not recommended and you might want to think for another approach.

Categories

Resources