Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

startService() from the service class itself

Tags:

android

I try to start an android service from the service's class. The reason to do this is to achieve some platform independence.

Doing this I get a NullPointerException at android.content.ContextWrapper.startService(ContextWrapper.java:326). The platform target is 2.1-update1, any suggestions?

See the code below (I left the imports out to save some space)

/* GUI: HelloAndroid.java */
package com.example.helloandroid;

public class HelloAndroid extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // that works!
        startService(new Intent("com.example.helloandroid.UnusualService"));

        // stop the service works of course, too
        stopService(new Intent("com.example.helloandroid.UnusualService"));

        // unusual start does not work:
        UnusualService myService = new UnusualService();
        myService.startService();
    }
}

/* Service: UnusualService.java */
package com.example.helloandroid;

public class UnusualService extends Service {    
    @Override
    public void onCreate() {
        Toast.makeText(this, R.string.service_started, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, R.string.service_stopped, Toast.LENGTH_SHORT).show();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null; // make something here when the rest works 
    }

    public void startService() {
        // folowing line will cause the NullPointerException
        startService(new Intent("com.example.helloandroid.UnusualService"));
    }

    public void stopService() {
        stopSelf();
    }
}
like image 207
vitamoe1.6 Avatar asked Oct 08 '10 07:10

vitamoe1.6


People also ask

What does the startservice method do?

The StartService method attempts to place the referenced service into its startup state. This topic uses Managed Object Format (MOF) syntax. For more information about using this method, see Calling a Method. This method has no parameters.

What is the difference between startservice and resumeservice?

The Win32_Service methods StartService and ResumeService should be used in the following situations: If a service is currently stopped, you must use the StartService method to restart it; ResumeService cannot start a service that is currently stopped. If a service is paused, you must use ResumeService.

How to start a service that has a start type of disabled?

You cannot start the services that have a start type of Disabled. If a Start-Service command fails with the message Cannot start service <service-name> on computer, use Get-CimInstance to find the start type of the service and, if you have to, use the Set-Service cmdlet to change the start type of the service.

How do I start a service from a service object?

You can specify the services by their service names or display names, or you can use the InputObject parameter to supply a service object that represents the services that you want to start. This example starts the EventLog service on the local computer.


2 Answers

Of course - your newly created service does not have a reference to a context, so the context is null and therefore the system throws a NullPointerException. Remember: Do not create a service on your own by using new - the system does this for you!

like image 101
mreichelt Avatar answered Sep 28 '22 17:09

mreichelt


Your UnusualService extends Service. Service extends ContextWrapper which contains Context. So when you call UnusualService.startService(new Intent("...")) it actually calls ContextWrapper.startService() which calls context.startService(). However at this point context is null and NullPointerException occurs.

public abstract class Service extends ContextWrapper implements ComponentCallbacks {
    private static final String TAG = "Service";

    public Service() {
        super(null);
    }

    //...
}


public class ContextWrapper extends Context {
    Context mBase;

    public ContextWrapper(Context base) {
        mBase = base;
    }

    @Override
    public ComponentName startService(Intent service) {
        return mBase.startService(service);
    }

    //...
}
like image 35
cement Avatar answered Sep 28 '22 18:09

cement