How to send data from BroadcastReceiver to an Activity in android? - android

I am writing an application to listen the SMS inbox in Android with one Activity and one BroadcastReceiver.Once the SMS comes the Receiver is showing Alert message...But i want to send the message information from Receiver to Activity.I don't know how to achieve this.Anybody knows it please help me...

I am writing an application to listen
the SMS inbox in android with one
activity and one BroadcastReceiver.
Please do not do this. This is not part of the Android SDK. Your application will break on some phones. Your application may break in future editions of Android.
But i want to send the message information from Receiver to Activity.
Send another broadcast Intent, this one a private one for use within your own application, where the Activity has registered a BroadcastReceiver (via registerReceiver()) for your private Intent.

You can add 'extras' to the Intent you use to start the Activity using the putExtra methods and retrieve the values using the getExtras method.
This mechanism should be used to pass small (meta-, or parameter-like) data to activities. For bigger data structures it is common to pass an Uri that identifies the data.

Related

Android/Xamarin: How to launch dialog with broadcast receiver

I want my app to listen for intends broadcasted by the call application, and when a call intend is broadcasted for a specific number I want to launch a dialog. I read that "A broadcast receiver may not display dialogs, and it is strongly discouraged to start an activity from within a broadcast receiver" https://developer.xamarin.com/guides/android/application_fundamentals/broadcast-receivers/ so I am assuming I should instead make the broadcast receiver launch a service that then launches a dialog. Can anyone confirm this? Also any simplified examples would be highly appreciated
Thanks!
In the example below, the app uses a BroadcastReceiver to detect a phone call number and decide whether it should answer or not:
How to reject incoming call programatically in android?
So using a BroadcastReceiver for that isn't that bad.
If you just want to show information about the phone call, you can display an Notification, as suggested by Jon Douglas in the comments. Displaying Dialogs from BroadcastReceivers isn't allowed (also disencouraged).

Detect when Notification is clicked to send analytic's event

We have an app with GCM notifications working fine, we set a PendingIntent so the activity that we want is open when the notification is clicked. However, we need to send an event to Google Analytics each time that one notification (and there are different types of notifications) is clicked. How could we achieve this? I don't want to parse the intent in the activity, as I think that this would not be a good solution (we are using TaskStackBuilder so not always the same activity is open for the same notification), is there a receiver we can use to detect when the notification is clicked/open?
Thanks in advance
put some flag variable on your notification intent.
intent.putExtra("notification","clicked");
Now check it in your Activity, whether Bundle is having "notification" key or not
if(getIntent().hasExtra("notification"))
//write your code for analytics
Try using NotificationListenerService. It allows an application to receive information about notifications. You need to declare the service in your manifest file with the BIND_NOTIFICATION_LISTENER_SERVICE permission and include an intent filter with the SERVICE_INTERFACE action to extend this class. See this example.
I also found this stackoverflow question on how to implement NotificationListenerService using TaskStackBuilder. This might help.

How can I send a list of supported intents?

I am creating a simple application that can be controlled by a service. In order to control this application, the service must know the commands that can send to it, so I thought it should be the application to provide a list of these commands, as follows:
The application may have a secondary activity that responds to the MY_ACTION_GET_COMMANDS intent (so I should define an intent-filter in the "AndroidManifest.xml" file).
The service knows that he can send this type of intent using the startActivityForResult method.
When the application receives this type of intent, the appropriate activity responds to the sender by sending a list of supported commands (ie, a list of intent).
How can I send a list of supported intents?
UPDATE: note that the application and the service are in different packages.
I am not sure I understand why u need to display an activity of these intents. Is this something that a real user will see?
If you really want to get list of all intents a particular app is listening to you can use PackageManager. Retrieve PackageInfo using PackageManager. Then you will be able to scan through all defined receivers in that app.
I may be able to help more if you explain a bit further.
You can pass the supported intents as list from the child activity back to the parent.
Intent intent = this.getIntent();
ArrayList<String> a = new ArrayList<String>();
a.add("com.customintent.INTENT_ONE");
a.add("com.customintent.INTENT_TWO");
intent.putStringArrayListExtra("list",a);
this.setResult(RESULT_OK, intent);

Service that broadcasts different message types

I want to make a service that I can register to. The service will broadcast messages of different "types" and each application can register itself to recieve messages of different type.
For example:
I want to write a servive that reads the twitter messages of some user and broadcasts to the systems the tags.
Then a consumer can register to recieve only messages of tag "foo", and recieve the tweet message.
Another consumer can register to recieve only messages of tag "bar", and recieve the tweet message.
Lets assume I know how to build a service. My first idea is to just broadcast a something, and then filtering it in the apps. But I am not happy about this solution. I know there are some android services that work similar to what I want, but I found no reference on the web on how to implement this.
Some RTFM I have done is:
http://groups.google.com/group/android-developers/browse_thread/thread/e1863d2822b22a33/90873ef925cd2aad
http://www.vogella.de/articles/AndroidServices/article.html
What is the simplest way to send message from local service to activity
How to have Android Service communicate with Activity -> not good for me, as this question is about a single process, and I need the service talking to several different processes
The main problem, is that most of the docs on the internet are about me calling the service, and not the service calling me.
Assuming you talk about Service.sendBroadcast(Intent): You can broadcast anything you want but it gets impracticable at some point. You could even define a new Action for each Twitter tag but that will just be complicated.
You could use the Data part of the Intents here, Receiving code could filter Broadcast like that:
IntentFilter ifilter = new IntentFilter("com.your.package.ACTION_TWITTER_MSG");
ifilter.addDataScheme("twitter");
ifilter.addDataAuthority("com.your.package", null);
ifilter.addDataPath("/foo", PatternMatcher.PATTERN_PREFIX);
and you send them like that:
Intent intent = new Intent("com.your.package.ACTION_TWITTER_MSG");
intent.setData(Uri.parse("twitter://com.your.package/foo"));
context.sendBroadcast(intent);

Android Bundle.containsKey throws Exception

I'm writing an Android Application. In this App the user should be able to recieve Notifications in the future. I'm using AlarmManager to send a Broadcast to my Reciever which has an Intent inside with the Notification as Parceled Extra. I don't want to put the whole Code here (its much), so I'm explaining just the workflow and put relevant code here.
I have extended Notification to let itself have an id and an Object from my Model (Model is Parcelable). I take care that the Notification gets Parcelled and unparcelled before my Model.
To send an Alarm I would create a new Notification Instance and call setLatestEventInfo with data from the Model. If I push this Notification directly, everything is fine.
After that, I create a new Pending-Broadcast-Intent. the Intent, which is put inside the Broadcast gets my Notification as ParcelableExtra. Then I fire that PendingIntent Via AlarmManager.set(). if time has come, I recieve the Broadcast in my BroadcastReciever.
I take the extras from my Intent and want to look, id theres a Key "NotificationExtra" which I used to store it. in that call I get an ClassCastException. If you are intereseted in some Code samples, feel free to ask. Any Idea what can go wrong? can I store null Values as Parcelables? And If I can store null values, can they be read properly?

Categories

Resources