处理程序不工作

时间:2014-01-03 10:06:54

标签: java android handler android-orientation

我在屏幕旋转时使用OrientationEventListenerHandler结合使用来更新TextView

  MainHandler handler;
TextView tv;
MyOrientationEventListener listener;
class MainHandler extends Handler
{
    @Override
    public void handleMessage(Message msg)
    {
        switch(msg.what)
        {
            case MSG_ORIENTATION:                   
                if(tv!=null)
                    tv.setText(msg.arg1);
                break;
            default:
                if(tv!=null)
                tv.setText("Nothing to see here");
        }
    }
}

这是我的OrientationEventListener

class MyOrientationEventListener extends OrientationEventListener
{

    public MyOrientationEventListener(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public MyOrientationEventListener(Context context,int attrs)
    {
        super(context,attrs);
    }

    @Override
    public void onOrientationChanged(int orientation) {
        // TODO Auto-generated method stub
        Log.d("MainActivity", "Orientation before rounding: "+orientation);
        orientation=roundRotation(orientation,0);
        Log.d("MainActivity", "Orientation after rounding: "+orientation);
        Message msg=handler.obtainMessage(MSG_ORIENTATION);
        msg.arg1=orientation;
        handler.sendMessage(msg);
    }

    public int getDisplayOrientation()
    {

        int rotation=getWindowManager().getDefaultDisplay().getRotation();
        switch(rotation)
        {
            case Surface.ROTATION_0:
                return 0;
            case Surface.ROTATION_90:
                return 90;
            case Surface.ROTATION_180:
                return 180;
            case Surface.ROTATION_270:
                return 270;
        }
        return 0;
    }

    public int roundRotation(int orientation,int orientationHistory)
    {
        final int ORIENTATION_HYSTERESIS=5;
        boolean changeOrientation=false;
        if(orientationHistory==ORIENTATION_UNKNOWN)
            changeOrientation=true;
        else
        {
            int dist=Math.abs(orientation-orientationHistory);
            dist=Math.min(dist, 360-dist);
            changeOrientation=(dist>=45+ORIENTATION_HYSTERESIS);
        }
        if(changeOrientation)
        {
            int nOrientation=((orientation+45)/90*90)%360;
            Log.d("OrientationEventHandler", "New orientation: "+nOrientation);
            return nOrientation;
        }
        return orientationHistory;
    }

}

我在onCreate中使用Handler将我的Handler注册到UI线程,因此它可用于更新屏幕上的Views。但是,这段代码似乎不起作用:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv=(TextView)findViewById(R.id.tv);
    handler=new MainHandler();
    listener=new MyOrientationEventListener(this);
}

我在调用各自的超类方法后,在listener.enable()onResume方法中使用listener.disable()。当我尝试旋转屏幕并进行舍入时:< / p>

onPause

1 个答案:

答案 0 :(得分:0)

试试这堂课:

class MainHandler extends Handler
{
    @Override
    public void handleMessage(Message msg)
    {
        switch(msg.what)
        {
            case MSG_ORIENTATION:                   
                if(tv!=null)
                    tv.setText(String.valueOf(msg.arg1));
                break;
            default:
                if(tv!=null)
                tv.setText("Nothing to see here");
        }
    }
}

我已经改变了

tv.setText(msg.arg1);

进入

tv.setText(String.valueOf(msg.arg1));