I've a little problem in my "timer" development.
I've an activity that shows a timer and when the user press start it schedules an alarm with AlarmManager service.
In the broadcast receiver I want to check if the activity Timer is running and visible in order to avoid unneeded notification.
So the broadcast receiver should set notification only if the activity isn't in foreground.
Any suggestion?
Thx,
SL3
You can use a Singletone pattern or SharedPreference property and store there some kind of state.
So, from your activity you can write the state and from your broadcast receiver you can read that state and then decide what to do.
Related
I'm trying to implement an app that times how long my phone screen is on throughout the day using a broadcast receiver.
I'm declaring Action_Screen_Off/Action_Screen_On in my broadcast receiver since I can't declare it in my manifest, and I've been debating on the best way to handle storing the amount of time that my screen was on.
Since I can't declare it in the manifest, Should I declare the broadcast receiver inside of the onCreate in my activity? My worry with that is, if my understanding is correct, is that my receiver would then be tied to the lifecycle of the activity and I would only be able to store the on/off times whenever the activity is active.
The whole point of the app is that it's working in the background, and then displaying graphs of usage once an activity is in the foreground.
This led me to think that a Service might be the best bet the handle the Broadcast receiver, but Google seems to now be recommending that we don't use background services, only bounded and foreground services.
How can I make sure that my app is receiving the on/off intents, without the activity that declares the receiver being in the foreground, and the receiver not depending on the lifecycle of that activity?
You need a background Service for this functionality. Your Service doesn't need to actually do anything, but it needs to be active all the time so that you have something to anchor your BroadcastReceiver to. In onCreate() of your Service, create an instance of the BroadcastReceiver and register for the screen on/off events. Make sure that you return START_STICKY from your onStartCommand() in your Service. This will ensure that the Service is always active, and Android will restart the Service if it kills off your process (for whatever reason). The BroadcastReceiver can just write the timestamps of the on/off events to a file, SQLite database or SharedPreferences and your Activity can then read this data and show the graphs or whatever.
I have created a Activity with few toggle buttons and based on the selection would like to perform specific operations at specific intervals. I have created the broadcast receiver and doing these activities in the onReceive(). I am using the alarm manager to run these tasks at particular intervals. If I don't un-register the recever in onDestroy I get an error saying there is leak, if I unregister the receiver in onDestroy it works fine but the receiver stops working.
I would like to keep the register listening even after the activity is closed, so that I keep performing particular tasks and specified interval. Please suggest
what you are looking for is a sticky broadcastreceiver. the best example i found so far is visible here --> http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html#broadcastreceiver_sticky
A normal broadcast intent is not available anymore after is was send
and processed by the system. If you use the
sendStickyBroadcast(Intent) method, the corresponding intent is
sticky, meaning the intent you are sending stays around after the
broadcast is complete.
My application consists of one activity which creates a service. I want the service to be keep running as long as application is running. I know:
It is not guaranteed as Android system can kill activity in low memory conditions and if activity is in background.
The service can be stopped (and killed) by system.
If I bind service to the activity, the activity would get notification in case service is being stopped or started. However, the service may stop running if activity goes in background (onStop()). Please correct me if I am wrong here.
If I bind to service in onResume() of activity and unbind() in onStop(), it might happen that service stops running when my application goes in background. If I bind in onCreate() and unbind() in onDestroy() of activity, would it mean that my activity will keep getting notification from service even when in background.
What is the best way to keep service running and get notification from service to Activity as long as application is running. Please note that there is just one activity in the application so sending activity in background means application goes in background.
Thanks
true
true, but its more rare if us use startForeground()
The service usually won't stop until all activities have unbound. But when the last has, it will. So u can prevent the service from dieing when going to background, if you only unbind in onPause if isFinishing() == true.
see 3.
I personally like to set up a Handler in the Activity and send Messages to it from the service.
If you are binding a Service to your Activity. It simply means that you need service to run as long as your activity is running. If you do not need to bind Service with activity or you do not need to update your UI while your Service is running. you must not bind your Service to your Activity. In this case, for different actions done by Service you can notify user using Android Notifications. Like notifying user that xx download has been completed.
It totally depends upon your purpose that you want to achieve from Service.
if you can use IntentService for your application, you can pass data to the service through an Intent. results can be passed back to the Activity through a ResultReceiver
If you bind your Service to your unique Activity, you'll have it alive as long as the Activity is not terminated or the service isn't unbound. Just bind it on the onCreate() and let it get unbound when stopping your activity (no need to do anything).
You can create a Listener interface within your service, that you'll implement in the Activity, so you can send those notifications from the Service to the Activity. You'll find suitable example and information about this if Googling.
Ok I am stuck. Can someone please point me in the right direction? I have no idea where to start with pendingintents. I need to start custom service that sends some data back to the activity that started it. How would I do that?
I would probably register a Broadcast Receiver in my Activity and if I needed to communicate from Service to Activity, send a broadcast from the Service and the Activity's receiver would pick it up as long as the Activity was currently running. This, by the way, would not require the use of a PendingIntent. PendingIntents are more used with alarms from the AlarmManager or with Notifications.
You should consider using AsyncTask if you expect your Activity to be active (visible) all the time the Service runs.
I want to write a service for Android platform that is notified when the current foreground activity changes.
Basically the service should do some tasks only when the top activity changes.
Is there any way to subscribe and to be notified when this kind of event occurs ?
Or there is no possibility and the service should poll from time to time the list of running activities and to check what is the foreground activity ?Not preferable solution...
AFAIK there are two ways to do that.
Start a service and monitor the Activity Stack, you might check it here
Use an Accessibility Service, you could find a solution here
You should bind every activity to the service and you will know which activity is running.
try this:
List runningTaskInfos=actvityManager.getRunningTasks(1).get(i).topActivity .getPackageName();
this method will give info of activity's package name which is in foreground..............