0

I have a application with activity and service.

       public class ClsService extends Service {
        private NotificationManager notificationManager;

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onCreate() {
            super.onCreate();

            this.notificationManager =  (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

            // other stuffs working great!
        }

        @Override
        public void onDestroy() {
            this.notificationManager.cancelAll();

            super.onDestroy();
        }
    }

    public class MyActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.mainactivity);

        CheckBox serviceEnabler = (CheckBox)findViewById(R.id.serviceEnabler);
        serviceEnabler.setChecked(true);
        serviceEnabler.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) {
                    startService(new Intent(this, MainService.class));
                } else {
                    stopService(new Intent(this, MainService.class));
                }
            }
        });
    }
}

My questions are:

  1. It's possible set up this service to run in other thread and not ui thread?
  2. If not, i need something to work on background fulltime even if activity is closed, this "something" exists, its possible?
  3. The way i create the service works as expected, if I close the activity the service keeps running but everytime I try stop then on activity I get NullPointerException on this.notificationManager.cancelAll();

    java.lang.RuntimeException: Unable to stop service com.idt.march.ClsService@45fabf30: java.lang.NullPointerException
    at android.app.ActivityThread.handleStopService(ActivityThread.java:3090)
    at android.app.ActivityThread.access$3700(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2099)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at com.idt.march.ClsService.onDestroy(MainService.java:155)
    at android.app.ActivityThread.handleStopService(ActivityThread.java:3076)
    at android.app.ActivityThread.access$3700(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2099)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
    
Lucifer
  • 29,392
  • 25
  • 90
  • 143
Trxplz0
  • 371
  • 1
  • 2
  • 13

2 Answers2

1

You can't run the service on another thread but you can create a new thread within the Service. The thread is created exactly the same way as you would in an Activity.

Regarding your NullPointerException try to add a check

if (this.notificationManager == null)
    this.notificationManager.cancelAll();

Although you might reconsider using cancelAll() because, according to the documentation, it would clear all notifications.

Merlevede
  • 8,140
  • 1
  • 24
  • 39
  • Thanks for answering, its might be good check if notification manager and other managers its different from null everytime i use? I read on documentation that all services can stop UI if their work are to high, how i can start a thread within the service? Thanks! – Trxplz0 Mar 07 '14 at 03:34
  • In my applications I check for nulls everywhere, and my apps on Google Play are rock solid, I haven't received any crash reports. It's a good practice. – Merlevede Mar 07 '14 at 03:38
  • *that all services can stop UI if their work are to high* doesn't ring a bell to me... your service might get killed, that yes! Check the START_STICKY flag in the documentation. – Merlevede Mar 07 '14 at 03:39
0

If you want to run a service in a background thread, consider using IntentService. It's not exactly the same as Service, but it is useful if it fits the kind of work you want the service to do.

Sofi Software LLC
  • 3,879
  • 1
  • 36
  • 34