Android線程間通訊的幾種方式


1.runOnUiThread(Runnable)              在子線程中直接使用該方法,可以更新UI

runOnUiThread(new Runnable(){//更新UI
                    @Override
                    public void run() {
                        publish_time.setText("更新失敗");
                    }
                    
                });

2.View.postDelay(Runnable , long)/new Handler().postDelayed(Runnable)

在需要更新UI的地方調用該方法,Runnable對象的方法里,直接操作UI;long是指延遲多少秒

//延遲一秒鍾出現
        new Handler().postDelayed(new Runnable() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                notify_view_text.setText(String.format(getString(R.string.ss_pattern_update), 10));
                notify_view.setVisibility(View.VISIBLE);
                //延遲兩秒鍾消失
                new Handler().postDelayed(new Runnable() {
                    
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        notify_view.setVisibility(View.GONE);
                    }
                }, 2000);
            }
        }, 1000);

 

 

3.使用Handler

 

4.使用AsyncTask


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM