Handling a Runnable thread during configuration changes

依然范特西╮ 提交于 2019-12-11 11:45:12

问题


With the following codes, I was wondering how to handle a Runnable object (for a timer) and a ViewTreeObserver (for handling configuration changes between portrait and landscape) together in one activity, because my View (linearLayout) doesn't get recognized until later:

MainActivity:

public class MainActivity extends AppCompatActivity {
    private int displayWidth, displayHeight;
    private LinearLayout linearLayout;

    // Initializes fields for the timer.
    private TextView timer;
    private long startTime = 0L;
    private long timeInMilliseconds = 0L;
    private long timeSwapBuff = 0L;
    private long updatedTime = 0L;
    private Handler customHandler = new Handler();
    private Runnable updateTimerThread = new Runnable() {
        @Override
        public void run() {
            timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
            updatedTime = timeSwapBuff + timeInMilliseconds;

            int secs = (int) (updatedTime / 1000);
            int mins = secs / 60;
            secs = secs % 60;
            int milliseconds = (int) (updatedTime % 1000);
            String localtime = "" + mins + ":" + String.format("%02d", secs)
                + ":" + String.format("%03d", milliseconds);
            timer.setText(localtime);

            customHandler.postDelayed(this, 0);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        setDimensions();
    }

    private void init() {
        linearLayout = (LinearLayout)findViewById(R.id.linearLayout);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.
            LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(params);

        timer = (TextView)findViewById(R.id.timer);
        startTime = SystemClock.uptimeMillis();
        customHandler.postDelayed(updateTimerThread, 0);

        setDimensions();
    }

    private void setDimensions() {
        ViewTreeObserver vto = linearLayout.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
            linearLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                displayWidth = linearLayout.getMeasuredWidth();
                displayHeight = linearLayout.getMeasuredHeight();

                System.out.println(displayWidth);
                System.out.println(displayHeight);
            }
        });
    }
}

Manifest:

<activity
    android:name=".MainActivity"
    android:label="@string/title_activity_main"
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:theme="@style/AppTheme.NoActionBar" >
</activity>

... And here's the output from logcat:

// Initial display of linearLayout in portrait mode.
04-03 03:22:31.215 4931-4931/dpark.sample I/System.out: 213
04-03 03:22:31.215 4931-4931/dpark.sample I/System.out: 277

// Landscape mode; View doesn't get recognized until later.
04-03 03:22:44.695 4931-4931/dpark.sample I/System.out: 213
04-03 03:22:44.695 4931-4931/dpark.sample I/System.out: 277

// Portrait mode with landscape dimensions.
04-03 03:22:56.625 4931-4931/dpark.sample I/System.out: 293
04-03 03:22:56.625 4931-4931/dpark.sample I/System.out: 197

// Landscape mode.
04-03 03:23:11.065 4931-4931/dpark.sample I/System.out: 213
04-03 03:23:11.065 4931-4931/dpark.sample I/System.out: 277

... and so on.

If anything, I could think of setting the Handler's postDelayed method to 100ms in run()... But, is there anything more efficient than that?

Thanks!

来源:https://stackoverflow.com/questions/36408277/handling-a-runnable-thread-during-configuration-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!