Skip to content Skip to sidebar Skip to footer

Trying To Start A Service On Android And Get A Null Object

I am creating an background service for android, so i call: startService(new Intent(this, RasPiDomBackground.class)); Service code: public class RasPiDomBackground extends Service

Solution 1:

In your Activity class

startService(newIntent(getBaseContext(), RasPiDomBackground.class));

In your Service class

publicclassRasPiDomBackgroundextendsService {

    /* (non-Javadoc)
     * @see android.app.Service#onBind(android.content.Intent)
     */@Overridepublic IBinder onBind(Intent intent) {
        // TODO Auto-generated method stubreturnnull;
    }

     @OverridepublicintonStartCommand(Intent intent, int flags, int startId) {
         // Let it continue running until it is stopped.
         Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
         return START_STICKY;
     }

     @OverridepublicvoidonDestroy() {
         super.onDestroy();
         Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
     }
}

Post a Comment for "Trying To Start A Service On Android And Get A Null Object"