Is it possible that If I place some part of the code from my Activity in Broadcast and then even if the activity is not running the broadcasted piece of code will continue to work?If yes any reference to it plz?
To b specific I've facebook activity that retrieves notifications on button click.I want that get notification method to b in a broadcast so that every time notification arrives it generates alert.Can provide the code aswel.. Kindly help
Is it possible that If I place some part of the code from my Activity
in Broadcast and then even if the activity is not running the
broadcasted piece of code will continue to work?
No, it is not possible that your activity is not running and you defined your broadcast in the activity and you still want the broadcast to run that is within that activity.
Add the BroadCast Receiver in a Service class. register and unregister this broadcast receiver from onCreate() and onDestroy() methods respectively of Service class.
Related
I have an android application where I am implementing push notifications using GCM. I have a broadcast receiver and an intent service for handling GCM Push notifications. My requirement is that I want to receive the notification inside OnReceive of the broadcast receiver in such a way that the Application doesn't get created before that.
According to Android Developer docs :
Called when the application is starting, before any activity,
service, or receiver objects have been created. Note that content
providers are created before the application object.
I have tried to start the broadcast receiver in a separate Global Process from that of the application, but on doing this the GCMBroadcastReceiver doesn't get triggered at all on sending a notification.
Is this possible at all, if yes how?
I want to receive the notification inside OnReceive of the broadcast receiver in such a way that the Application doesn't get created before that
That is not possible, sorry.
I am developing an Android app that need to change the interface when a push notification arrives,
Example:
User start the Activity A, then He goes to Activity B.
While User is reading a text in Activity B,a Push notification arrives, the text in Activity B should change and the text in Activity A should change too.
I can do it on Activity A with onResume() but I dont know how to do it in Activity B
The GCM sample that comes with the GCM library (in your Android SDK Folder/extras) gives you an example on how to do this:
in GCMIntentService onReceive method, they called displayMessage, which broadcasts a message after a GCM notification is received.
in DemoActivity::onCreate, they register a receiver to handle the broadcast message.
the onReceive method of the broadcast receiver mHandleMessageReceiver performs the UI update.
If you want to receive updates when notification arrives then you need to implement Broadcast Receiver. You need to register the receiver in onResume and unregister it in onPause.
The place you need to implement to update your text views is the onReceive method of Broadcast listener. You can easily find some examples by searching "Android Broadcast Receiver". Hope it helps.
I have an application that is launched from a broadcast receiver, but I don't want to launch the application if the user is currently in the middle of a phone call. How would I check from my broadcast receiver class if there is currently a phone call going on?
You should create a service with a PhoneStateListener. Make sure it gets unregistered once you get what you need from it and make sure not to launch the listener directly from your broadcast receiver. There are some more details concerning this concept in this question.
I'm currently messing up with the Google's C2DM notification service.
Following the steps in this tutorial: http://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html, I succesfully recieved a "push message" from the server.
However, in the "protected void onMessage" I need to send the message to the "MainClass" to print it in a toast. Since I'm not deeply familiarized with the Android developing, I will appreciate any help on this. Thank you
Use a broadcast to communicate with the activity.
In onMessage send a broadcast.
In your activity onResume register a broadcast receiver and make it display a toast (remember to unregister it in the onPause)
You would need also to handle the case when the activity is not running (maybe display a notification). In this case, make the broadcast an ordered broadcast. The broadcast receiver in the activity should be set with a high prio, then register a default broadcast receiver through your manifest (this one displays a notification, or opens the activity, or whatever you want).
I want to run some code when an app is launched, so my broadcast receiver has to be notified when user open any app.
Is there any way to do it?
No, sorry, there are no Intents broadcast when an application is started, nor when an activity is started.
Probably you can have some trigger from a method like onCreate etc which would be the first method to be hit whenever your app is launched.
This is possible using accessibility services. An event AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED is recieved when the foreground activity is changed. The package name of the app in the foreground can be obtained from within the callback.
I have provided the code snippet in the answer to a similar question here.
How to monitoring app swaping in foreground?
The complete code for sample app to get the foreground process name is also available at https://github.com/abinpaul1/Android-Snippets/tree/master/GetForegroundService