change imageview in the whole app - android

I have an app that is a chat, while you navigate in the App, you can receive a message, if you receive it the icon "message" changes to a new one (that new icon tells the user that he has a new message).
while you are navigating in the app I created different Activities that have the same footer (include other layout)
So what I want is, how I can change the state of the message to new message in all the activities when I receive the message?
I thought about creating an interface with one method that checks if the activity has been created and if is, just change the imageView, but I think there is a better way.
Any Idea?

You might consider creating a interface that have methods based on different kind of updates.
For example onNewMessage(Message) and similar. Then you have let the implementors register themselves with the service that determines when these events occur. So when a message is received the service will go through the list of registered implementors and post the update.
The implementor would in this case update it's imageview.
Don't forget to also unregister when reasonable and so forth.

Related

Android - keep activity in background when clicking local notification

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.

service sending string to another activity without opening it

I have a service that is working in the background - SERVICE A.
When I open an activity - ACTIVITY A, I want this service to change the text that is displayed on the editText field of this ACTIVITY A.
I thought that maybe if I get the context of the opened activity then I will be able to use and work on its Views. Is it possible?
Also a related question:
If I don't have the name of the field, can I use some loop to run and search for all the views in the activity and get its properties?
It seems like a less direct method would be preferred to getting this, rather than have the activity be directly updated by the service. In fact, in general Service/Activity communications are best handled in one of the following ways:
MessageHandlers- Basically, one can send a message to the other, which in turn the other sets the actions of the text.
BroadcastReceivers (LocalBroadcastReceiver is also okay)- One part sends a broadcast message to anyone listening, the other sets up a listener for that message. An Intent is passed, which can contain the message.
I would suggest that you use the second. The application sends a broadcast to the service upon opening, and the service returns a broadcast to pass the requested data to the Activity.
See also the Service docs from the Android SDK.
1 save the string in preferences from service (see this : https://stackoverflow.com/a/12074248/4647577 )
2 retrieve the string when activity is created and set it to edittext
In your question you said that you want to show the string when you open the activity , so the 2 steps above will do the job .
For better performance you can repeat the second step in onresume();

Prepare an activity before show

The problem is only when my activity already shown it begins updates and it look no good. How it works: in onStart of activity I send cmd to service to get update data, also I register brodcast listener there. I want to prepare received data from service to show before activity appears. How to do that? Thanks.
How it works now: when I back from another activity first I see old data and then it changes (very fast but you can see it) to new.
if you want to setup things before your activity is shown you need to do things in the onCreate method instead of onStart method.
further informations in android documentation
When you send the command to the service to update your data, you should change the views in your Activity to show loading indicators, or a blank view, or whatever you want the user to see until the Broadcast comes in that your new data is ready. Then you shouldn't need to worry about old data being visible.

Controlling number of AppWidget instance

I want to create a widget for my android app that display some real time details about locations e.g. weather. but i want to allow max 3 instances of the widget at any point of time, each with a different location. I am not sure how to go about it and can't find any information.
my questions are:
Is there a limit on number of appWidget instances that could be created?
How to go about limiting the number of widget instances user can create at any point?
there are multiple ways to count the app widgets that i can think of:
send an intent to all of your app widgets to see that they still exist, and count them, and only in the end you would know how many there are. however, i'm not sure how you would know when to stop counting.
not sure if it works but maybe you could use:
   
final ComponentName cn=new ComponentName(context,YourAppWidgetProviderClass.class);
int[] appWidgetsIds=appWidgetManager.getAppWidgetIds(cn);
and check the size of the result.
update the counter based on receving the ACTION_APPWIDGET_DELETED (or ACTION_APPWIDGET_DISABLED) and ACTION_APPWIDGET_BIND (or ACTION_APPWIDGET_ENABLED) actions in the intents on the onReceive() method of your AppWidgetProvider class.
anyway, if there are too many (4 or more in your case) , just show an alternative layout for the new widget.
you would also need to handle the case of removing app widgets (those that weren't disabled) so that you would enable new ones (and decrease the counter), or you could let the user click on disabled ones to trigger the check again.
about limiting the creation itself, i don't think it's possible, but as i wrote, it's possible to count them and disable new ones.
do note that the user might have the app widget available on more than one app . it doesn't even have to be a launcher that will show your app widget. also not sure how it would work in case there are multiple users (available on android 4.2 and above) .

How can one get the appwidget's own ID?

I am trying to debug a problem where my widget becomes invalid and a new one fills in. This only happens once at the beginning of time and then it remains the second widget forever. So, I want to put code in to narrow down the point in time when it happens. What I would like to do it something like this:
e.g., (not real code) Log.d(TAG, "myWidgetId=" + this.getMyWidgetId());
Then I would get a list of all the enabled IDs and see if I am in the list. I cannot find a method, member, attribute, etc. that would give me my id?
It cannot be done. The AppWidgetProvider's ID is implicitly handled and can change, therefore, the best one can do is to ask the AppWidgetManager to generate a list of the widgets for a particular component, invoke them, then respond to the service request using the provided ID.

Categories

Resources