Android的 - 我怎么能停止服务里面的线程? [英] android - how can I stop the thread inside the service?

查看:171
本文介绍了Android的 - 我怎么能停止服务里面的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MainActivity选中的按钮。如果该按钮被选中它应该启动该服务,但如果用户取消选中按钮我想停止该服务。

因此​​,在取消选中状态我写了这个 stopService(intentname); 但问题是服务没有停止。这里是我的code片断:

服务类

 公共类SimpleService延伸服务
{
    串selectedAudioPath =;
    私人MyThread的myythread;
    公众意向的意图;
    公共布尔isRunning = FALSE;    长间隔= 30000;    @覆盖
    公众的IBinder onBind(意向为arg0)
    {
        返回null;
    }    @覆盖
    公共无效的onCreate()
    {
        super.onCreate();
        myythread =新MyThread的(间隔);
    }    @覆盖
    公共同步无效的onDestroy()
    {
        super.onDestroy();        如果(!isRunning)
        {
            myythread.interrupt();
            myythread.stop();
            isRunning = FALSE;
        }
    }    @覆盖
    公共同步无效在onStart(意向意图,诠释startId)
    {
        super.onStart(意向,startId);        如果(!isRunning)
        {
            //this.intent =意图;
            //System.out.println(\"the原意即为+意向);
            myythread.start();
            isRunning = TRUE;
        }
    }    类MyThread的继承Thread
    {
        长区间;
        公共MyThread的(长间隔)
        {
            this.interval =区间;
        }        @覆盖
        公共无效的run()
        {
            而(isRunning)
            {
                的System.out.println(服务运行);
                尝试
                {
                    字符串的myString = intent.getStringExtra(名称);
                    如果(myString的== NULL)
                        Log.d(服务,空);
                    其他
                    {
                        Log.d(服务,不为空);
                        如果(myString.equalsIgnoreCase(图像))
                        {
                            uploadImages();
                            视频下载(间隔);
                        }
                        否则如果(myString.equalsIgnoreCase(音频))
                        {
                            uploadAudio();
                            视频下载(间隔);
                        }
                    }
                }
                赶上(InterruptedException的E)
                {
                    isRunning = FALSE;
                    e.printStackTrace();
                }
            }
        }


解决方案

您无法停止,有一个正在运行势不可挡的循环这样一个线程

 ,而(真)
{}

要停止该线程,声明一个布尔变量在while循环条件下使用。

 公共类的MyService扩展服务{
      ...
      私人线程MyThread的;
      私人布尔运行;     @覆盖
     公共无效的onDestroy()
     {
         运行= FALSE;
         super.onDestroy();
     }     @覆盖
     公共无效调用onStart(意向意图,诠释startid){         运行= TRUE;
       MyThread的=新主题(){
       @覆盖
       公共无效的run(){
         而(运行){
                   MY code至润;
                 }
         }
       };
     };
     mythread.start();}

来源:停止服务内螺纹

I have a checked button in my MainActivity. If that button is checked it should start the service but if a user unchecked the button I want to stop the service.

So in uncheck condition I have written this stopService(intentname); but the problem is the service is not stopping. Here is my code snippet:

Service Class

public class SimpleService extends Service 
{
    String selectedAudioPath = "";
    private MyThread myythread;
    public Intent intent;
    public boolean isRunning = false;

    long interval=30000;

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

    @Override
    public void onCreate() 
    {
        super.onCreate();
        myythread  = new MyThread(interval);
    }

    @Override
    public synchronized void onDestroy() 
    {
        super.onDestroy();

        if(!isRunning)
        {
            myythread.interrupt();
            myythread.stop();
            isRunning = false;
        }
    }

    @Override
    public synchronized void onStart(Intent intent, int startId) 
    {
        super.onStart(intent, startId);

        if(!isRunning)
        {
            //this.intent = intent;
            //System.out.println("the intent is" + intent);
            myythread.start();
            isRunning = true;
        }
    }

    class MyThread extends Thread
    {
        long interval;
        public MyThread(long interval)
        {
            this.interval=interval;
        }

        @Override
        public void run()
        {
            while(isRunning)
            {
                System.out.println("Service running");
                try 
                {
                    String myString = intent.getStringExtra("name");
                    if(myString == null)
                        Log.d("Service","null");
                    else
                    {
                        Log.d("Service","not null");
                        if(myString.equalsIgnoreCase("image"))
                        {
                            uploadImages();
                            Thread.sleep(interval);
                        }
                        else if(myString.equalsIgnoreCase("audio"))
                        {
                            uploadAudio();
                            Thread.sleep(interval);
                        }
                    }
                } 
                catch (InterruptedException e) 
                {
                    isRunning = false;
                    e.printStackTrace();
                }
            }
        }

解决方案

You can't stop a thread that has a running unstoppable loop like this

while(true)
{

}

To stop that thread, declare a boolean variable and use it in while-loop condition.

public class MyService extends Service {
      ... 
      private Thread mythread;
      private boolean running;



     @Override
     public void onDestroy()
     {
         running = false;
         super.onDestroy();
     }

     @Override
     public void onStart(Intent intent, int startid) {

         running = true;
       mythread = new Thread() { 
       @Override
       public void run() {
         while(running) {
                   MY CODE TO RUN;
                 }
         }
       };
     };
     mythread.start();

}

Source: Stopping a thread inside a service

这篇关于Android的 - 我怎么能停止服务里面的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆