문제

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)
    
도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top