当我使用setRequestedOrientation时,不会在线程中更新volatile布尔值的Android值

时间:2014-06-18 04:57:40

标签: android multithreading

我试图在SurfaceViewer上运行一些测试,SurfaceViewer有自己的线程,一切正常。然后我使用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)更改了我的代码以更改屏幕方向; 在下面的代码中:我有一个使用Fragments的活动。我有另一个扩展SurfaceView类的类。添加setRequestedOrientation时问题就开始了。添加setRequestedOrientation后。在FastRender类运行方法中运行的值始终为false。如果我使用setRequestedOrientation对行进行注释,则会运行罚款。

package com.example.testpractise;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.AssetManager;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

public class SurfaceViewTest extends ActionBarActivity {

    static int counter;
    static {
        Log.i("Intializing class Surface Test","counter");
        System.out.println("Statrting here");
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        counter++;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        counter++;
        Log.i("Counter vlase",String.valueOf(counter));
        System.out.println("value of counter"+counter);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_surface_view_test);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.surface_view_test, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        FastRenderView renderView;

        public PlaceholderFragment() {
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {


            //View rootView = inflater.inflate(
                //  R.layout.fragment_surface_view_test, container, false);
            getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            BitmapCollection.initializeBitMapCollection(getActivity());
            renderView = new FastRenderView(getActivity());
            return renderView;
        }



        @Override
        public void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            renderView.resume();
        }
        @Override
        public void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
            renderView.pause();
        }
        static class FastRenderView extends SurfaceView implements Runnable {
            Thread renderThread = null;
            SurfaceHolder holder;
            static volatile boolean running = false;
            int x=0;
            int y=0;


            public FastRenderView(Context context) {
                super(context);
                // TODO Auto-generated constructor stub
                holder = getHolder();
            }

            public void pause()
            {
                running = false;
                while(true)
                {
                    try{
                        renderThread.join();
                    }catch (InterruptedException i){

                    }
                }
            }

            public void resume()
            {
                running = true;
                renderThread = new Thread(this);
                renderThread.setName("RendererThread");
                renderThread.start();
                running=true;
            }
            @Override
            public void run() {
                // TODO Auto-generated method stub
                int sleepCounter=0;
                while(!running)
                {
                    try {
                        if(sleepCounter <5)
                        {
                            sleepCounter++;
                        Thread.sleep(5000);
                        }else{
                            break;
                        }
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                while(running)
                {
                    if(!holder.getSurface().isValid())
                    {
                        continue;
                    }
                    Canvas canvas=holder.lockCanvas();
                    canvas.drawRGB(255, 0, 0);

                    holder.unlockCanvasAndPost(canvas);
                }

            }

        }
    }

}

很抱歉,如果我的问题与格式不符。这是我第一次。任何有关错误的指示以及为什么会受到高度赞赏。

谢谢

1 个答案:

答案 0 :(得分:0)

如果您只想将Activity锁定到横向,可以通过将android:screenOrientation="landscape"属性添加到activity元素来在Manifest中执行此操作。