在何处“退出"?用弯针吗? [英] Where to "quit" with looper?

查看:117
本文介绍了在何处“退出"?用弯针吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个弯针问题.我叫looper.prepare(),在做完所有事情后一切正常.但是,如果我旋转设备,则准备过程会出现异常.

I have a problem with a looper. I call looper.prepare(), and after doing something it all works fine. But if I rotate the device I get an exception on the prepare.

07-12 16:40:09.760: E/activity(15809):  java.lang.RuntimeException: Only one Looper may be created per thread

我正在尝试退出循环播放器,但是它什么也没做.

I'm trying to quit the looper, but it doesn't do anything.

这是我的AsyncTask:

Here is my AsyncTask:

 @Override
    protected String doInBackground(String... args) {

        try{Looper.prepare();   //here start the exception

       try {  

            URL  url = new URL(link); 
            HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
         conn.setDoInput(true);   
            conn.connect();  
            InputStream is = conn.getInputStream();
          utente.measure(0, 0);
            bmImg = decodeSampledBitmapFromResource(is,(int) utente.getMeasuredWidth(), utente.getMeasuredHeight(), link);

 if(bmImg!=null){


        try{  

         getCroppedBitmap();
        }catch(Exception e){
            System.out.println(e);
        }

          }

        }
        catch (IOException e)
        {       
           Log.e("lele", "errore qui");
            e.printStackTrace();  

        }
        Looper.myLooper().quit();   //do nothings
        }catch(Exception e){
            Log.e("canta tu",  " "+e);
        }
        Looper.myLooper().quit();  //do nothings
        return null;   
    }
        @Override       
protected void onPostExecute(String args) {

            //Looper.myLooper().quit();   //generathed an error, main thread can't stop looper

       if(bmImg!=null){ 
           try{

           utente.setImageBitmap(bmImg);
           ellisse.setVisibility(View.VISIBLE);

           }catch(Exception e){
               Log.e("lele",""+e);
               Log.e("lele","errore probabile out of bound");
           }

           }
       else {

           Toast.makeText(getApplicationContext(), "Modifica la foto da \"profilo\"", Toast.LENGTH_LONG).show();
       }

想法?

推荐答案

有两种情况需要考虑:

(1)个Looper线程,这些线程要在应用程序的整个生命周期中都存在,并且不持有对视图的强烈引用(即使不是隐式的)

报价 Google工程师克里斯托弗·泰特(Christopher Tate)-您可以在您的应用程序被销毁之前,将弯针放在那里,它将随之消失.您不必担心.

Quoting Google engineer, Christopher Tate - you can just leave the looper there until your app is destroyed, and it will go down with it. You don't need to worry about it.

通常来说,永远不要退出循环程序线程.该方法主要是出于历史和测试原因.在Real Life™中,我建议您在整个生命周期内继续重复使用相同的循环程序线程.处理而不是创建/退出它们."

"Speaking very generally, never quit() your looper threads. That method exists mostly for historical and testing reasons. In Real Life™, I recommend that you continue to reuse the same looper thread(s) for the life of the process rather than creating/quitting them."

我将这样的循环程序线程用作多用途 HandlerThread ,并在我希望某些东西在主线程(UI)之外运行时将Runnables发送给它.

I use such a looper thread as a multi purpose HandlerThread, and send Runnables to it whenever I want something to run outside the main thread (UI).

(2)个引用了视图的弯针线程

这个不符合克里斯托弗·泰特(Christopher Tate)的建议,因为它会导致内存泄漏,例如,如果旋转屏幕.
(您最好将处理程序线程设为静态并使用弱引用-然后会返回选项#1)
要杀死它,您必须退出循环.为此,您需要在该线程的上下文上运行quit命令.
因此,请使用msg.what作为int值创建一个消息,并在handleMessage中等待该int消息以及消息何时到达-调用:

This one falls out of the recommendation of Christopher Tate, because it will cause memory leak, for example if you rotate the screen.
(You better make the handler thread static and use weak reference - and you'll be back with option #1)
To kill it you must quit the loop. To do that, you need to run the quit command on the context of that thread.
So create a message with some whatever int as your msg.what, and in your handleMessage wait for this int, and when it arrives - call:

Looper myLooper = Looper.myLooper();
if (myLooper!=null) {
    myLooper.quit();
}

也不要忘记将所有对视图和活动的引用为空.

And don't forget to null all reference to views and activities.

将此取消消息从您的活动onDestroy()

Send this kill message to the handler from your activity onDestroy()

这篇关于在何处“退出"?用弯针吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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