When MyService gets called, I want to add a ProgressBar to a LinearLayout that is currently set to an instance of MyActivity. How can I get the view/layout from within MyService?
The way I've done this in the past is to use broadcast a message from the service and have the Activity have a BroadcastListener listening for that message. The payload of the message can contain the value of the progress bar.
Related
My app receives FCM message and sends a local notification. When the app is running with other activities on top of the MainActivity, I don't want the notification to start a new MainActivity or bring it to the foreground. I need it to stay at bottom of the stack, check the data in the local notification and show a dialog to the user. Is there any way to do this?
Or, any way to make whatever the activity on top of the stack to handle the local notification?
Well you can achieve this by creating a base activity, which you will inherit in all of your application's activities. When you receive an event, you simply check if the activity that is at the top of the stack is an instance of your base activity or not.
If it is, you can create a dialog in the base activity with the data that you received in the event.
Hope it helps.
I sent a broadcast to my app instead of starting activity when local notification is clicked. The broadcast receiver checks for the top activity and performs some tasks accordingly.
I need to use the following line in service class
getWindow().getDecorView().setSystemUiVisibility(8);
Please anyone help.
A service can not set window properties, since it has no view/UI. You must send an event to your activity, which must set this property. When it comes to sending the event to activity, you can use several mechanisms:
Use a messenger which you passed from activity to service earlier
Use AIDL interface
Easiest : Send a broadcast Intent from your Service class, let your Activity listen for that broadcast, and set the action on UI thread
I'm receiving intents in a broadcast receiver (declared in manifest), details of which I am 'logging' via b'cst intents received by my 'MainActivity', whose receiver updates the contents of a text view and is registered within the MainActivity code.
I'm wondering if there's a way to keep the contents of the MainActivity text view updated even if the MainActivity doesn't have 'focus' (i.e. another activity has been started). I appreciate that the b'cast receiver of the MainActivity will become unregistered upon pausing, but feel there ought to be a way to do this.
Any ideas?
You should do all of that on the "onResume" of the activity, just when it gets to the foreground.
One of solutions is to use a handler inside receiver, and in that you call a static method inside your activity...inside that method you can update or whatever you need to do with your text view...
As it doesn't appear to be possible to put an EditText in an AppWidget, I would like to open a PopUpWindow with an EditText when I click on it.
I know how to open an Activity from an AppWidget and I also know how open a PopUpWindow from an Activity. I don't, however, know how to open a PopUpWindow from an AppWidget. I've looked into many classes in the javadoc (Intent, RemoteViews, PendingIntent, etc.), but I can't find how to start this PopUpWindow. Any help would be appreciated.
You know that AppWidgetProvider is a BroadcastReceiver.Android Doc says:
A BroadcastReceiver object is only valid for the duration of the call
to onReceive(Context, Intent). Once your code returns from this
function, the system considers the object to be finished and no longer
active.
This has important repercussions to what you can do in an
onReceive(Context, Intent) implementation: anything that requires
asynchronous operation is not available, because you will need to
return from the function to handle the asynchronous operation, but at
that point the BroadcastReceiver is no longer active and thus the
system is free to kill its process before the asynchronous operation
completes.
In particular, you may not show a dialog or bind to a service from
within a BroadcastReceiver. For the former, you should instead use the
NotificationManager API. For the latter, you can use
Context.startService() to send a command to the service.
It seems you have three ways:
Use a service to show popup(see How to display alert diaolog(popup) from backgroung running service?)
Use notification manager(see AlarmManager never calling onRecieve in AlarmReceiver/BroadcastReceiver).
Create an activity whit dialog theme(so it looks like a popup) and display it when user click your AppWidget.
You could have the appWidget open an activity which then shows a dialog fragment, or make the activity look like a dialog using a dialog style.
I have to publish the progress from a background service on the UI continuously on a progress bar. Any ideas on how to go about it. Intents won't work I guess coz they can only send the data once the activity is started. Ant other suggestions?
Update : The progress on the UI happens on a progress Bar
Extend Application, which is created once for entire application.
When Activity starts, store its reference to a field in your Application object. (Note that you can access Application using Activity.getApplication). Set this field to Activity reference or null in onPause/onResume calls.
Then in Service, you have also access to your Application by Service.getApplication. So look if your Activity reference is non-null, meaning that your Activity is shown to user, and update UI as needed in such case, by calling methods on your Activity.
Thanks for the help mice, but since I needed to update progress bars on different activities in my app depending on which one was visible, I found it easier to implement through Broadcast Intents and Recievers and Intent Filters. All I had to do was to Broadcast the progress in my service wrapped up in a bundle via a broadcast Intent (with a custom Intent Filter applied) and register (in onResume()) an inner subclass of BroadcastReciever in the activities which needed the progress (having the same intent filter). One can also unregister these recievers in the onPause() method of the activity to save memory headspace.