Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在另一个事件中停止线程_Android_Multithreading - Fatal编程技术网

Android 在另一个事件中停止线程

Android 在另一个事件中停止线程,android,multithreading,Android,Multithreading,我的问题是。。。 我有两件事。。onCreate和Onclick。我有一个线程在onCreate中运行,我必须在onClick中停止它。这可能吗? 如果是,请向我提供一些提示/代码片段,说明如何实现这一点 onCreate事件中的代码如下:- protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activi

我的问题是。。。 我有两件事。。onCreate和Onclick。我有一个线程在onCreate中运行,我必须在onClick中停止它。这可能吗? 如果是,请向我提供一些提示/代码片段,说明如何实现这一点

onCreate事件中的代码如下:-

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_screen);

    button = (Button) findViewById(R.id.btn);
        button.setOnClickListener(this);
        try{
            bright = Settings.System.getInt(getContentResolver(), 
                     Settings.System.SCREEN_BRIGHTNESS);
            }catch(Exception ex){
                ex.printStackTrace();
                bright = 1.0f;
            }


        lp = getWindow().getAttributes();
         new Thread(new Runnable(){

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(10000);
                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            lp.screenBrightness= dim;
                            getWindow().setAttributes(lp);
                            // SLEEP 2 SECONDS HERE ...
                            final Handler handler = new Handler(); 
                            Timer t = new Timer(); 

                            t.schedule(new TimerTask() { 
                                    public void run() { 
                                            handler.post(new Runnable() { 
                                                    public void run() { 
                                                    lp.screenBrightness=bright;
                                                    getWindow().setAttributes(lp);
                                                    } 
                                            }); 
                                    } 
                            }, 2000); 

                        }
                    });
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }

        }

      }).start();  

    }

此活动正在循环内运行。我想停止这一切,我点击按钮。似乎我无法在button onclick事件中调用Thread.interrupt()方法。我该如何处理这件事

如果创建引用,则可以从类中的任何位置访问它。所以,在班级集体广告中

Thread thrd 
就像你用一个按钮,文本视图,或者其他什么来做一样。然后在onCreate方法中添加如下引用

thrd = new Thread(new Runnable(){
现在您有了一个线程的引用,您可以在类中的任何地方使用它

  • 在类中声明一个
    boolean
    mrunning
    ,这就像一个标志,自定义运行线程将依赖于此来知道它是否已被取消

  • 通常,线程中有一个循环,而不是
    while(true)
    do
    while(mRunning)

  • 另外,您正在使线程休眠,在休眠期间,线程将无法检查
    mRunning
    变量,您必须调用
    interrupt()
    on thread对象使其停止

  • 最好使用一个变量来保存线程引用
    thread t=new thread(..

  • 因此,现在要停止线程,必须调用
    mRunning=false
    ,然后调用
    t.interrupt()

  • 而不是
    while(true)

    在run()方法中,将布尔变量声明为true,并在onClick之后更改它。例如:

         private boolean shouldRun = true; 
    
    然后在run()方法中:

    在你的onClick中:

            @override
              public void OnClick(View v){
    
                  shouldRun=false;
                          .
                          .
                          .
    

    根据您的当前情况,根据之前的回答评论。您正在尝试:

    myThread = new Thread(new Runnable
    
    对吧?

    然后你还应该删除先前编码的一部分。然后写:

    myThread.start();
    
    你应该在你的类中将myThread声明为全局线程,这样你就可以从你的类中的任何地方访问这个线程。现在你可以在
    onClick中停止线程


    你应该把这个答案和
    User117
    s的答案结合起来。

    @Bhuro:我已经更新了我的问题。无论如何,谢谢你的链接。这样,创建一个处理程序、一个计时器和一个计时器任务似乎有点过分了。可以省略计时器和计时器任务,只需执行
    Handler.postDelayed(new Runnable(){..},2000)当我尝试这个myThread=new-Thread(new-Runnable(){时,当我使用线程引用myThread=new-Thread(new-Runnable…)时,我会收到这样一条消息“类型不匹配:无法从void转换为Thread”。当我使用线程引用myThread=new-Thread时,我会收到这样一条消息:“类型不匹配:无法从void转换为Thread”(new Runnable…,我收到一条显示“类型不匹配:无法从void转换为线程”的消息
    
    myThread.start();