-2

I have a class that extends Application

public class MyApp extends Application
{
    @Override
    public void onCreate()
    {
        super.onCreate();
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
        Log.i("Try","App ID = " + intent.getStringExtra("MyID"));
    }
}

I also have a Service that generates an ID.

public class MyService extends Service
{
    public void onStartCommand(Intent intent, int flags, int startId)
    {
         intent.putExtra("MyID", getID);
    }

    public String getID()
    {
        //code to generate ID
        return ID;
    }
}

My problem is I need to pass the ID from MyService to MyApp but the Log states "App ID = null".

Shizuka Masuda
  • 89
  • 3
  • 13

2 Answers2

0

You have a couple of options:

  1. Bind to the service and get your ID by calling mService.getID() (see: http://developer.android.com/guide/components/bound-services.html to learn about bound services);

  2. From your service send a local broadcast passing the generated ID in intent extras. Register a BroadcastReceiver wherever you would like to catch the broadcast (see example code: how to use LocalBroadcastManager?).

Community
  • 1
  • 1
dev.bmax
  • 8,998
  • 3
  • 30
  • 41
0

There are many options:

  • Using the service object itself (as singleton).
  • Using AIDL.
  • Using Intents.
  • BroadCast Receiver
Pang
  • 9,564
  • 146
  • 81
  • 122
mdDroid
  • 3,135
  • 2
  • 22
  • 34