Starting Android IntentService with explicit inten

2019-05-16 08:36发布

问题:

I'm trying to start an IntentService from a clickhandler in my main activity. I'm studying Intents right now but don't quite get it yet. I'm not sure how I'm supposed to instantiate my intent here and pass it to startService. I don't know why I have to do such a thing, the Intent will not be used within my service, that I know of. I don't know how to debug this problem.

From the click callback:

    this.commute = new Commute();

    locationService = new LocationService();
    locationService.setCommute(commute);
    // com.orm.SugarApp@8b2e18f is this.getApplicationContext() ...
    Context context = this.getBaseContext();
    System.out.println("!!!!!!!!!!!!!!!!!!!!!");
    System.out.println(context);
    Intent intent = new Intent(context, LocationService.class);
    System.out.println(locationService);
    System.out.println(intent);
    locationService.startService(intent);  // <---- OFFENDING LINE
    System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@");

    chronometer.setBase(SystemClock.elapsedRealtime());
    chronometer.start();
    commute.start();

logcat before traceback, none of the objects are null...:

01-27 09:53:44.465    2718-2718/org.skyl.commutetracker I/System.out﹕ !!!!!!!!!!!!!!!!!!!!!
01-27 09:53:44.466    2718-2718/org.skyl.commutetracker I/System.out﹕ android.app.ContextImpl@8b2e18f
01-27 09:53:44.466    2718-2718/org.skyl.commutetracker I/System.out﹕ org.skyl.commutetracker.services.LocationService@387dcc1c
01-27 09:53:44.466    2718-2718/org.skyl.commutetracker I/System.out﹕ Intent { cmp=org.skyl.commutetracker/.services.LocationService }
01-27 09:53:44.466    2718-2718/org.skyl.commutetracker D/AndroidRuntime﹕ Shutting down VM
01-27 09:53:44.481    2718-2718/org.skyl.commutetracker E/AndroidRuntime﹕ FATAL EXCEPTION: main

This causes the following traceback:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ComponentName android.content.Context.startService(android.content.Intent)' on a null object reference
            at android.content.ContextWrapper.startService(ContextWrapper.java:515)
            at org.skyl.commutetracker.MainActivity.startCommute(MainActivity.java:86)
            at org.skyl.commutetracker.MainActivity.toggleClick(MainActivity.java:67)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)

回答1:

You would not want to instantiate LocationService on the second line.

You would want to use one of the following:

context.bindService(intent, serviceConnection, boolean);

or

context.startService(intent);

For your IntentService, you would want to use startService. Bound services are different than intent services.

Also, if you want to pass the commute into the intent service, you would want to pass it into the intent as an extra.

intent.putExtra(String key, Parcelable item)

The putExtra is overloaded, so as long as the Commute object implements one of the required interfaces, it should work fine. More information about the Intent object can be found at http://developer.android.com/reference/android/content/Intent.html

You can find more information about services here http://developer.android.com/guide/components/services.html



回答2:

you do not create an object of the service to start it all you do is simply use context to start it

Intent intent = new Intent(context, LocationService.class);
context.startService(intent);