Passing data from receiver to activity - android

I am really stuck. After test lots of different approaches. I'm asking this question.
I'm trying to make an app which should alert the users at the specific date and time, like lots of other apps that you have definitely seen before. I'm using BroadcastReceiver as it should. I register it in manifest to activate it the first time the app gets installed and after restarting the phone but the problem is in android 3.1- taskkillers can kill this receiver so I thought it would be better to activate it again each time the app gets opened but The problem is that I don't know how many instance I'm creating so the first question is:
How to get the active receiver?
so I can make a decision upon it. If it is not active so I can active it again.
What I'm doing within onReceive is: getting data from database and comparing the time and date to now. If the app is not open I want to notify the user in notification area and pass extra data to the app' which is working pretty well. But if the app is open I do not want to notify the user in notification area, instead I want to pass data to an activity and alert the user in my app. I made my activity singleTask and used startActivity to pass data to the activity also I used onNewIntent method to handle new data but the problem is what if the user is using another activity. The second and third questions are:
How to know if my app is open? (I used ActivityManager.getRunningTasks but I realized it is not a good solution because it is an api for Task Manger apps.)
how to pass data from receiver to my activity? (I used interface to pass data from fragment to activity so I thought it can be used here but it does not work here)
After a lot of exertion -Reading and Trying- I know i'm still doing wrong So please guide me.

I suggest that you start a ServiceIntent from the Broadcast reciever. and from there you can send an intent that you can catch in the activity

Okay no one answered my question but I got it myself so here is my own answer:
Every receiver has only one instance and you can just enable or disable it. So to ensure that it is working you can enable it in your activity every time it gets run.
Within onReceive instead of doing stuff there, start a service and in your activity bind the activity with this service so in onBind and onUnbind method you can change a Boolean value and make decision upon it. In onBind make the value True which means your app is open and vice versa in onUnBind.
And finally the best way to pass data from service to activity is using Interface.
Now my app is working just like I want. . Hope to be helpful and save someone's time.

Related

adding function into notification

hello guys probably my question is pretty basic but since am a noob in android development have to ask it , i want to add an action inside a notification and am almost done with it by using this code
notification.addAction(R.mipmap.ic_launcher, "Decrease", pendingIntent);
but as you can see i added a pendingIntentin parameter and this intent is passing me to a particular activity (what it supposed to do so) but instead of passing the user to an activity i want to perform an action without opening the app how can i do this ?? and my action is for increasing a counterValue like performing Counter++
Pending intent can only used as a intent, without such ability to run only a piece of code.
You can use a dummy activity to be opened on notification click and exits immediately after doing the jobs.
However, using activities can be annoying for the users as it flashes in and out on screen. So you might want to use Services to not interrupt users and do your job simultaneously.
In your case IntentService seems to fit which can be used with ease.
Android document nicely describes a service's lifecycle and methods of it so it's worth a try.

Android Starting 2 activity at a time causes issue

I have number of activities in my app. From front end I can start different different activities but I have back end as well, that means from server if i received a message than app has to take action on the message and start the activity based on the message.
My problem is that sometimes app received message from the server and app starts activity and at the same time user also performs click on UI and navigates to other activity. In this case one of my activity is not started as android can't start both activity at a time.
Is there any INTENT LAG which can help to resolve this issue?
Right now what i am doing is if i received message from server than I am using one global flag and using that flag i am avoiding such situation, but I am looking for better solution if anyone has any idea on this.
Your solution sounds fine. There really isn't a good way to do this because Android discourages this behaviour. If your application requires this behaviour you will need to create a workaround, which it sounds like you have done.

Using an intentService or are there better ways?

I'm creating an app that will do the following:
If a mail comes in with a messageword. It needs to start to locate the phone by GPS functionality. after that it must show the result to the user.
I already have it working with using a broadcastreceiver for fetch the message. After that is starts an Activity. If don the userinterface is updated.
I have here two problems:
1) if the screen rotates all variables are set to zero... well can solve that with saving variables at onPause() and read those values at onResume()
2) if the user use the backbutton... the thread is cancelled.
Actually I want that the user can't cancel the GPS-action.
Is it a good idea to use an Intentservice for this?!
-message comes in
-BroadcastReceiver fetch the message and start the intentService
-if the processing is done... I can start an activity with the results (and the service is closed automaticly after the processing is done?!
Is there maybe a better way to attack the problem?
You certainly can and should use an IntentService for long running operations in the background. However, if the user backed out of your activity, they most probably did this for a reason, and popping up an activity with results they might not care about anymore, might not be such a good strategy. You could cache the result instead, and show it next time the user opens your activity.

Is there any possibility to send a parameter between two activities at a time interval?

I want to pass a parameter between two activities at a certain time interval. The passed parameter is an int, that represents the number of satellites seen by the device and is used to draw a chart in the second activity.
There is no harm in keeping a GpsStatus Listener in both of those activities provided that you stop GPS properly when not needed. :)
But anyway, If I were you I'd let the Application class hold on to these details. I'd use a Service to listen to the GpsStatus and the Application class bind to this service. Your Application class is now the gateway. Your activities can now smoothly communicate with the Application data, instead of the clumsy Intents.
This pattern is taken from here
It is very likely that you are trying to do something in a strange way because you do not fully understood yet how activities in Android are working.
There is only one active Activity in your whole app. Sending data from one activity to another is only needed if the users changes the active Activity, either through clicking on a button and switching to a new Activity or through clicking back and going back to the previous Activity. Please read the Activity Lifecycle very carefully to understand how activities should be used.
I assume that you have registered a location listener in one activity and try to use the values of this listener in other activities. Normally you would use the onPause method to deactivate the listener in the first activity and then activate a new one in the new activity. This will get you the number of known satellites in every activity. If the user goes back you will have to reregister the listener in the onResume method to reenable the gps updates for the new Activity.
Keeping an active GPS Listener in a paused Activity will lead to an active GPS Sensor even if your App is in the background and not used anymore. This will drain the battery of the phone very fast and very likely have your app uninstalled in an instance.
You could use a shared preference to store the int in a kind of variable that both activities can access.
Otherwise, try a custom intent and implement a receiver in one or both Activities. This is a little harder to implement, but with the receiver you can act as soon as the value changes.
If it is rather important to do it in the time interval, use a shared preference ;-)
You can also pass bundles between two activities. That Intent can be used for storing
parameter values in PutIntent.
More information here: http://remwebdevelopment.com/dev/a33/Passing-Bundles-Around-Activities.html

update application state(global variables) from broadcastreceiver

I've been searching and reading and not fond a concise answer that fits what I need so hopefully you guys can point me in the right direction.
I have a simple app which needs to periodically check the web for results. Reading around, it seemed using an AlarmManager was the most appropriate way to schedule an event to happen as opposed to a background service.
The main activity shows the most recent result of the web query, when that query took place, and the next time it's due to re-query.
THe problem I'm having is that I didn't realise the BroadcastReceiver ran in a separate process...when the receiver updates the applications last query result, last check time and next check time, it's obviously not doing it in the copy of the object the app uses ;-) so it looks stale when I open the app...
I've tried Static variables on a StateManager class that are set from the broadcastrecevier and main activity, i've tried specifying the StateManager to be an Application subclass and specifying that as the android:name in the manifest...both seem to end up with distinct copies so the state updated from the broadcastreceiver isn't the state that the app sees.
NOw I'm beginning to understand what's happening...I'm struggling to understand the best way to resolve it.
Using a SQLLite DB to persist state is going to be too mcuh trouble because I want to store an object graph and it appears you can't do that yet (even if SQLLite stores blobs, the android interface doesn't cover it yet ?). I obviously don't want to spend hours on O-R mapping either.
I 'Do' currently raise a notification through the notificationmanager when the web query highlights a need, and that allows me to pass the intent and extras through to the app..so that part works for me...however, when the webquery returns a result that doesn't need notifications generated, I have no 'path' back to the application to give it the next check time, and I dont' want to bring the app forward just for the sake of having a way to pass extras to it.
I guess the crux of the problem is that I want,
from a broadcastrecevier, to persist an object somewhere that my app can retrieve when it's live
or
have a broadcastreceiver perform work within the app process (without bringing the app to the foreground) so that any static variable changes are made to instances that the activity use.
if the best way is to write this as a service, then so be it (if that's the case, before I start coding..does the service run in the same process as the application or will I get similar 'it's not doing what a singleton was supposed to do' problems that I've been having with the broadcastreceiever!)
Seems I'd specified android:process=":remote" in the receiver section of the app manifest. Removing that makes the Alarm run int he apps thread which resolved the issue.

Categories

Resources