10

I have a Service in it's own process, I've declared it in manifest like that:

   <service
        android:name="com.discountscatcher.util.PointService"
        android:configChanges="orientation"
        android:process=":pointservice" >
    </service>

But onStartCommand I was trying to get an ApplicationContext, but it always returns null, what I can forget?

    public int onStartCommand(Intent intent, int flags, int startId) {
    android.os.Debug.waitForDebugger();
    Context mContext = getApplicationContext();
    return Service.START_STICKY;
}

and I starting it like that:

    startService(new Intent(getApplicationContext(), PointService.class));

in Myactivity OnCreate

any ideas?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
whizzzkey
  • 926
  • 3
  • 21
  • 52
  • I think what you need is Interprocess communication http://developer.android.com/guide/components/processes-and-threads.html#IPC – George Daramouskas Dec 19 '13 at 08:04
  • 2
    A service is a Context, why do you need the application context? – FunkTheMonk Dec 19 '13 at 09:49
  • @FunkTheMonk how i can get an application resources like strings, drawables etc? – whizzzkey Dec 20 '13 at 02:39
  • @whizzzkey the same as you would in an Activity / Application - Service extends from Context - you can just call getString(), getResources().getDrawable() etc – FunkTheMonk Dec 20 '13 at 09:49
  • 1
    @FunkTheMonk no man, it seems to be some differs between application context and service context when service has started as separate process and when i call getString() or any other resource it always returns me null. – whizzzkey Dec 20 '13 at 10:03

1 Answers1

8

You are running your Service in a different process so it has no communication with the Actual Application process..

Try to remove this line from your service

android:process=":pointservice"

After your application is finished Application context is becomes null. At most situations there is no need to run the service in a different process. Also it is not recommended.

For more information check here

  1. Service in another process
  2. service reference Example2
Community
  • 1
  • 1
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59