BroadcastReceiver notification and activity messages - android

I'm very new to Android and I need a little assistance here.
I have an activity and a broadcast receiver.
Broadcast receiver should listen SMS Intents and:
if Activity is visible and interacting - SMS should appear in activity
if no - broadcast receiver should create notification with SMS, so if user click on that notification, the activity will be shown
So, i read a lot of articles about broadcasts and i can implement one of these variant(1 - as a inner broadcastreceiver class and 2 - as global broadcast receiver declared in manifest) , but how i can have both? Could you please propose some idea?

Ok, so in that case i post my own solution.
I declare two broadcast receiver: one, declared in Manifest, and doing post to tray notification, and second - declared as an inner class of main activity and doing post to activity components. Also i have subclass of Application class to track main activity state (i.e. visible or not). And so global broadcast receiver post any notifications only if it see that activity is hide right now.
I think its reasonably solution.

Related

Should BroadcastReceiver be declared inside activities?

I'm wondering what's the better cleaner design approach to handle BroadcastReceiver creation between those two:
Declaring directly a BroadcastReceiver in an Activity/Fragment and overriding onReceived method there.
Creating another custom receiver class in a package "receiver", that extends the BroadcastReceiver class and override onReceive. Then, you instantiate that custom receiver class in your Activity/Fragment.
There are 2 types of Broadcast Receivers
1)Dynamic Broadcast Receiver that is registered inside activity
2)Static Broadcast Receiver that is registered or declared inside Manifest file with Receiver tag with specific action(when to fire the Receiver)
Uses:Eg say receive message
1) Dynamic Broadcast Receiver is used to receive the Broadcast intent mainly to refresh the message sent through send Broadcast method.
2)Static Broadcast Receiver is used to receive the message.Here the Registered Receiver with SMS receive action will receive the message and later it sent to activity
At last It depends on developer which one to use,when to use based on requirement
There is no cleaner approach, it depends on how you Code.
Following the DRY principle, I only extend a BroadcastReceiver as soon as I notice I'll need them in different Activites.

android - update specific activity UI from onReceive

I have a BroadcastReceiver, and the onReceive is called from two different postExecute methods in two different asyncTasks, in two different Activities.
I have a third activity that is running all the time called HomeActivity, and I want to publish some text to the HomeActivity's UI from the onReceive method.
Is it possible? I know that the context parameter is the context of the activity who raised the onReceive, but I want to access the HomeActivity's UI.
Here is the code
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// here I want to publish some text to the HomeActivity
}
}
any ideas? thanks in advance
You want to change text in your running activity based on what you receive in the onReceive of the BroadcastReceiver? Right? One way is that you can use LocalBroadcast. See LocalBroadcast Manager.
For how to implement is, there is a great example on how to use LocalBroadcastManager?.
LocalBroadcast Manager is a helper to register for and send broadcasts of Intents to local objects within your process. The data you are broadcasting won't leave your app, so don't need to worry about leaking private data.`
Your HomeActivity can registers for this local broadcast. From the MyBroadcastReceiver you send a LocalBroadcast from within the onReceive (saying that hey, I received a message. Do you want to do something now activity). Then inside your Activity you can listen to the broadcast. This way if the activity is in the forefront/is active, it will receive the broadcast otherwise it won't. So, whenever you receive that local broadcast, you may change the text etc, if activity is open.

Parse.com Push notifications and ordered broadcast

I'm implementing Parse push notification. I have 2 receivers. First one will always show notification and second one will take over notification if one particular activity is running. One registered in manifest (priority 1) and the other is registered/unregistered dynamically (priority 2). My problem is that I cant cancel the broadcast from my dynamic receiver by calling
abortBroadcast()
it throws an exception
BroadcastReceiver trying to return result during a non-ordered broadcast
So, is there a way to make the broadcast ordered or something like that?
I really want to handle the data in my activity when it is active.
Ok, after 2 hrs of searching, finally solved it. This is what I did,
First I created a base BroadcastReceiver which receives the push notification from Parse. It then removes all actions from received intent and adds a custom action eg, com.myclass.PUSH
Then I used
context.sendOrderedBroadcast(intent, null);
to send a new ordered broadcast using my custom action.
Now I set other two receiver's (the one defined in manifest and my dynamic receiver defined in activity) action to com.myclass.PUSH
Now the broadcast is ordered and i can cancel it using abortBroadcast()

Start Activity for result in a Broadcast Receiver?

I have managed to get an Activity to start from my onReceive() methdod, but I really need to do a startActivityForResult();.
Is there any way I could do this?
On a side note, how would I make my app become a 'camera' app, as in it would appear when an app started the intent to take a picture?
The important thing to know about broadcast receivers is that you should not add long running processes in it, because after something like 5 seconds your app will crash.
The best thing to do in your case is to intent to other Activity from your broadcast receiver, and from that activity use startActivityForResult(), get the picture and continue from there...
startActivityForResult can only be called from an Activity since it is defined in the Activity class and require instance of activity.
You can only call startAcivity() from broadcast receiver since in onRecieve() you only have access to generic context object and it does not have startActivityForResult method defined in the class..

How BroadcastReceiver is different from Intent

Hello I see contradicting definitions. Android experts, can you explain this to me please?
1) If BroadCastReceiver is a component in android that responds to intents, then I can as well register an filter for activity in androidManifest xml file and have it do my job based on intent like battery low, no network, orientation change etc. these are intents I might be interested to react in my code.
2) Why register whole another filter for BroadcastReceiver in androoidManifest.xml and perform action at onReceive() inside the BCRCVR class?
3.Can we really perform intent driven operations in an activity? yes right?
I guess the title should be "How BroadcastReceiver is different from Activity".
In my opinion, Broadcast receiver is independent unit, because sometimes you don't want the system to create a new Activity object just to handle arrived intent. Moreover, you don't your activity to be shown. Broadcast receivers are independent and can be used outside any Activity. Activity is more about user interface and broadcast is about handling events.

Categories

Resources