0

I have a service that places a red rectangle on the screen and has a onDestroy() that looks like this

@Override
    public void onDestroy() {

      Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show(); 
}

from one of my activities i am calling to kill this service like so:

 stopService(new Intent(this, assNavOverrideService.class));

i see the toast come up saying that the onDestroy() has been called but i still see the red rectangle and it is still running..

my manifest is using :

<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" >

myService

public class assNavOverrideService extends Service {

    Timer timer;
    WindowManager.LayoutParams handleParams;

    View screenBlock;
    View disableStatusBar;
    WindowManager winMgr;
    @Override
    public void onCreate() {

        winMgr = (WindowManager)getSystemService(WINDOW_SERVICE); 
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        disableStatusBar = new TableLayout(getApplicationContext());
        handleParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.FILL_PARENT,
                50, 
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                 // this is to keep button presses going to the background window
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
                 // this is to enable the notification to recieve touch events
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                 // Draws over status bar
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                PixelFormat.TRANSPARENT);

         handleParams.gravity = Gravity.TOP;
         handleParams.y = 750;

         disableStatusBar.setBackgroundResource(R.drawable.fake_nav);

         winMgr.addView(disableStatusBar, handleParams);

         timer = new Timer();
         uiCheckTask tTask = new uiCheckTask();
         tTask.setService(this);
         timer.schedule(tTask, 50, 10);

        // If we get killed, after returning from here, restart
         return START_REDELIVER_INTENT;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // We don't provide binding, so return null
        return null;
    }

    @Override
    public void onDestroy() {

      Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show(); 
    }



    class uiCheckTask extends TimerTask {
        Boolean secDialog = false;
        assNavOverrideService myService;


       public void setService(assNavOverrideService mS){
           myService = mS;
       }


        public void run() {

                Log.v("TICK","TOCKKKKKKKKKKK");


        }



    }



}
erik
  • 4,946
  • 13
  • 70
  • 120
  • when i get answers that work i accept them.. come on man – erik Oct 24 '12 at 22:35
  • I am just telling that some people (like me) will skip such questions. 50% of 5 question is perfectly fine, but of 75 is not that nice. I just do not want to spend my time and efforts to be 76th. Just a note - do what you want, but maybe you are asking your question wrong, unclear way? – Marcin Orlowski Oct 24 '12 at 22:37

2 Answers2

0

Firstly you don't need to have that permission to stop a service that you create. I use services frequently (starting and stopping) and I assure you I don't ever use that permission.

ie:

Intent service = new Intent(myContext,Service.class);
    
    
    startService(service);
    
    
    stopService(service);

is all you need.

Of course your service does need to be declared in your Android Manifest.

Now anything that affects your activity uses the context inside that activity. So killing your service won't change what's on the UI. In your services on Destroy, you should need to somehow reference the rectangle object and remove it that way.

I have no idea how you created the rectangle. But if it is a view, you could set it to VISIBILITY.GONE, or if it's a ViewStub, you can deflate it, if it's an object you can clear it, etc..

If you are saying your service is still running even after you call the stop service function, then something else is wrong. Even though it looks like you are stopping the service correctly.

----------------------------- EDIT ---------------------------------------------------------------------------------------------------------- Your service might also be bound to your activity too. If you are doing this, you need to release the binding.

Here's a link for a similar question:

I hope this helps you! Cheers

Community
  • 1
  • 1
Dave
  • 3,178
  • 5
  • 28
  • 44
  • The rectangle is in a system overlay drawn by the service in the oncreate method of the service. I should mention that the service is also starting a timer task that is an inlined class – erik Oct 25 '12 at 12:01
  • sorry i mean it draws the system-overlay in the onStart – erik Oct 25 '12 at 12:13
  • I have not called bindService() is it possible it is binding itself? cause i have not manually binded it to anything – erik Oct 25 '12 at 12:40
0

Solved, in my Service's onDestroy() i had to stop the timer task and the service then closed...

erik
  • 4,946
  • 13
  • 70
  • 120