【问题标题】:How to manually stop a service?如何手动停止服务?
【发布时间】:2017-12-22 06:45:19
【问题描述】:

我试图停止我的服务,即使我的 destroy 方法也执行了,但我的服务没有停止。我已经尽力了。请帮帮我。

我的代码如下:

主活动

  1. 意图意图=新意图(this,Myservice.class);
    启动服务(意图); //在开始按钮中。

  2. 意图意图=新意图(this,Myservice.class);
    停止服务(意图); //在停止按钮中。

我的服务

public class Myservice extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this,"runing",Toast.LENGTH_LONG).show();

     {
         myTherad th=new myTherad();
         Thread thread=new Thread(th);
         thread.start();


     }

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

}

@Override
public void onDestroy() {
    Toast.makeText(this,"stop",Toast.LENGTH_LONG).show();
    super.onDestroy();

}

class myTherad implements Runnable {

    @Override
    public void run() {
        synchronized (this)
        {
            try {

                for(int i=0;i<10;i++)
                {
                    wait(1000);
                    Log.d("mfnhsdgjkfn","===="+String.valueOf(i));
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        stopSelf();


    }


}

}

【问题讨论】:

  • this answer
  • stopService(new Intent(this, MyService.class));并根据需要在服务的 OnDestroy 方法中处理服务停止过程
  • stop service in android的可能重复
  • 只是为了检查-您了解停止服务不会停止您的服务启动的线程,对吗?停止服务的代码不知道存在线程。你必须自己做。

标签: android android-service


【解决方案1】:

我认为您的服务已停止,但不是您的 Thread。通过引入例如取消您的线程以及您的服务的onDestroy某种状态变量:

 class myTherad implements Runnable {
     // Indicates that the runnable should stop. 
     private boolean shouldStop = false;

     @Override
     public void run() {
        synchronized (this)
        {
            try {

                for(int i=0;i<10 && !shouldStop;i++)
                {
                    wait(1000);
                    Log.d("mfnhsdgjkfn","===="+String.valueOf(i));
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        stopSelf();


  }

并将您的可运行对象声明为类属性:

private myTherad runnable;

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

    runnable=new myTherad();
    Thread thread=new Thread(runnable);
    thread.start();

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

} 

并在您的onDestroy 中停止运行:

@Override
public void onDestroy() {
    Toast.makeText(this,"stop",Toast.LENGTH_LONG).show();
    runnable.shouldStop = true;
    super.onDestroy();

}

至少在最新的wait 执行后,你的线程应该停止。您可以在中断的情况下对其进行优化。

【讨论】:

    【解决方案2】:

    我们必须插入正在运行的线程,thread.interrupt() 会这样做并提供java.lang.InterruptedException,试试下面的代码:

    public class MyService extends Service {
    public MyService() {}
    myTherad th;
    Thread thread;
    
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    @Override
    public void onCreate() {
        super.onCreate();
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this,"runing",Toast.LENGTH_LONG).show();
    
        {
             th=new myTherad();
             thread=new Thread(th);
             thread.start();
        }
        return super.onStartCommand(intent, flags, startId);
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this,"stop",Toast.LENGTH_LONG).show();
        thread.interrupt();
    }
    
    class myTherad implements Runnable {
    
        @Override
        public void run() {
            synchronized (this)
            {
                try {
    
                    for(int i=0;i<10;i++)
                    {
                        wait(1000);
                        Log.d("LOG","===="+String.valueOf(i));
                    }
    
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
            }
            stopSelf();
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多