如何停止服务?

时间:2014-08-20 07:16:15

标签: android

在我的应用中,我正在使用该服务。我正在面临停止服务和线程两者的问题。在stopself()方法中,不调用onDestroy()。由于代码很长,我已经发送了代码的结构。

请建议我任何解决方案。

 public class MonitorService extends Service {

   public MonitorService()
    {
    }
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        Log.e("THREAD", "onCreate()");
        super.onCreate();
    }
    @Override
    public void onStart(Intent intent, int startId) {
        Log.e("THREAD", "onStart()");
        id=startId;
        super.onStart(intent, startId);
    }
    @Override
    public void onDestroy() {
        Log.e("THREAD", "onDestroy()");
        super.onDestroy();
        Thread.currentThread().interrupt();

    }

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


        Run a = new Run(app, time,sec,startId);//a[i]=new Run(app, time,sec);
        Log.e("APPTIME",app+ " "+time);
        Thread t=new Thread(a);
        t.start();

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    class Run implements Runnable
    {

        Run(String app,int time,int sec,int startId)
        {

        }

        @Override
        public void run() {

        while(true)
        { 

            if(packageName.equals(pack))
            {

                      handler=new Handler(Looper.getMainLooper());
                      handler.post(new Runnable() {  
                          @Override  
                           public void run() {  
                              blocked=true;
                              helper.setSate(app, "BLOCKED");
                           }  
                        });

                 }
                 else
                 {
                      sec=sec+1;
                      String secs=Integer.toString(sec);
                      Log.e("Left",secs+" "+pack);
                      if(sec==limit)
                          {
                            sec=0;
                            block=true;
                          }
                 }
            }
            }//cmin1=day
            if(cmin==day)
            {


                handler=new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {  
                      @SuppressWarnings("deprecation")
                    @Override  
                       public void run() { 
                          Log.e("cmin=day","Ready for notification");

                          Notification1 n1=new Notification1(getApplicationContext());

                          // need to stop service and thread over here


                       }  
                    });
                 stopSelf(startId);
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }  //end while
        }  //end rur

    }
}

0 个答案:

没有答案