Send data to MainActivity from Service - java

I have a lookup service which calculates the latency of some servers. I want to write those in TextView but since there is no findviewbyid in Service i have to send those datas to main activity. Basicly i want to send an array to MainActivity from service. How can i do that?

I think you wanted to transfer data from one activity to another? So just use the startActivityForResult()
https://developer.android.com/training/basics/intents/result

Related

Suggestions for transferring int value from Service to MainActivity?

I have a Service that is started from a pendingIntent.
This service gives either a 1 or 2 int (there's a notification that contains a button, that once pressed returns 1 then 2 over and over.) Need to transfer this int to my mainActivity to then say (in my mainActivity)- if (ServiceClass.getNum ==1) then mToggle.setChecked(true) else (false).
I heard a bunch of different suggestions on how to do this. Some of those suggestions include Binding to the activity, using a Local Broadcast receiver? Also my mToggle button needs to still be able to change states even if the Activity is in the background or closed.
Any suggestions on the best course of action is appreciated.
For communicating between Android activity and service we can use broadcast receiver.
you can see this https://developer.android.com/guide/components/broadcasts

At what moments can I get info from an intent?

I send an intent with information to start an activity. Now, instead of retrieving the info in the onCreate method, can I retrieve it after that moment? For example, when I click a button in the new activity?
Yes I think you can if you write it this way:
YourActivity.this.getIntent().getExtras();
getIntent().getExtras() yields the data in your current Activity.
If you want to pass this data to a new Activity you have to read them and write into a new Intent.
If the first activity is made as singleton then you can get the Intent data from the only Activity's instance.
Or you can make another singleton class just to hold that data for you.

Send String Data without Changing Activity(Intent)

I am trying to send the string from one Activity to Another WITHOUT changing the CURRENT ACTIVITY. This my code I used:
Intent intent = new Intent(this, AnotherActivity.class);
intent.putExtra("getFollowerNumberData", txt);
startActivity(intent);
Using this code bring me to the other activity or I do not want that. I just want to send this string without changing activity. I tried this one
Intent intent = new Intent();
But it is crashing my app. First of all it is possible to do so? If yes, how can I achieve that?
Edit: To be more clear, it works like follower and Following features in other apps or games. When you follow someone the number of the person you just followed goes up as well your Following number. Since my SignInPlayerProfile.Class(where the Following should go up to one too) is in another activity I was trying to get this information intent.putExtra("getFollowerNumberData, txt); from Main Activity and display that in the SignInPlayerPlayer by using text.setText(). But the Problem is by using intent = new Intent(this, AnotherActivity.class); it brings me to SignInPlayerProfile Activity which I do not like since
I am trying to send the string from one Activity to Another WITHOUT changing the CURRENT ACTIVITY(or simpler words WIHTOUT Going to the Other Activity).
Thanks for Help.
When an activity getting closed, according to it's life-cycle, it will be destroyed and no longer exists, when you start an activity it will be created and then it can get your data, so you can't send data to an destroyed activity which no longer exists
You can use Static Variables to communicate between activities (Classes), you can change value of a Static-Variable of an activity from another activity, but it's not a good option for data you need to be alive because Static Variables lives on Heap Memory and Heap will be freed if Android OS needs more Memory
Another way is to create a Message Handler in your first activity as a Static Variable and then send a Message to the Handler from second activity, see this example :
http://stacktips.com/tutorials/android/android-handler-example
I suggest you using SharedPreferences for saving your data in first activity and load from it on second activity
EDIT :
According to your edit, the "Number" you want to use in another Activity as "Following" or "Followers" is just needed when the second activity is visible, you should use sharedpreferences to save the "Number" and load from it when you need it. For example before text.settext() method you can load the number from sharedpreferences and then pass it to text.settext()
You should not save your data on the variables or classes and should save them on a file like a Database or SharedPreferences then you can load them every time you want
Furthermore you can search about Activity life-cycle and see how to use life-cycle events like OnStart to load your data
Firstly please consider startActivityForResult() ,we can send information from one activity to another and vice-versa. As mentioned consider using shared_preferences for local in memory storage. To truly accomplish this feat in a elegant way though, do consider using obervables(rxandroid). You publish observations in one activity or fragment ,
then subscribe in another activity or fragment. I did not mentioned event bus nor otto since rxjava/rxandroid surpasses it. They act a promise context management system. Also because observable in process dependent consider using broadcast receivers, to broadcast events through out your application and external if so desired.

Android how to pass pointer on List and receive it back

I'm developing a simple Application. It has 3 buttons (start record, stop record, send info to the server). I want to record my voyage on the map, and I implemented it, but it works only when Activity is onResume(), so now I implementing sticky service, which will do exactly the same, but in the background. So service will fill ArrayList<MapPoint>, and then I have to receive it back somehow to my activity. It is not necessary to pass ArrayList to Service, but I have to receive it back to send it to the server in onDestroy() method. Please, help.
I thinks you can put the value in shared preferences and then pull it back in the activity

How to Pass data that changes based on time from Activity to Service

What's the best way to pass data that is changing based on time from the Activity to Service?
Or the best way to initially pass it and handle it from a Background Service?
Thanks,
You can use simple Bundles or for after usage u can store data locally using SharedPreferences
more info:
http://www.vogella.de/articles/AndroidServices/article.html

Categories

Resources