-2

I am currently using Android Network Service Discovery. With the function below, each time it runs, it returns one host name. However, I want to store all data somewhere to pass to the next activity, how will I be able to that?

public void onServiceFound(NsdServiceInfo service) {

            Log.d(TAG, "Service discovery success" + service.getServiceName());
            if (!service.getServiceType().equals(SERVICE_TYPE)) {
                Log.d(TAG, "Unknown Service Type: " + service.getServiceType());
            } else if (service.getServiceName().equals(mServiceName)) {
                Log.d(TAG, "Same machine: " + mServiceName);
            } else if (service.getServiceName().contains(mServiceName)){
                mNsdManager.resolveService(service, mResolveListener);
            }
        }

Example I have 3 names to store, how would I be able to do that?

Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
  • if you don't need them to persist, then just add it as an extra to the intent before you launch the new activity. if you want them to persist, then use sharedpreferences. – Angel Koh Sep 26 '17 at 03:36
  • If the function will run multiple times for multiple names (eg. 50 names), how can I intent all of the names at once? – Tiew Wei Jian Sep 26 '17 at 04:05
  • Since you are using internet, I guess, then you can also store your info in Firebase or you can your application can create a file, where all the information can. be stored. The other option is that you can also use shared preferences. – Akhil Nair Sep 26 '17 at 04:14

1 Answers1

0

Every time you get a name just add it into a list, e.g. myListOfNames.

just before spawning a new activity, add that list into your intent as an extra.

you can do something like this

Intent i = new Intent()
i.putExtra("my_names", TextUtils.join(",", myListOfNames));
startActivity(i);

where TextUtils.join will collapse your list into a single comma separated string.

Angel Koh
  • 12,479
  • 7
  • 64
  • 91