第3課第1節_基礎知識Android訊息處理機制

來源:互聯網
上載者:User

標籤:center   ems   ifstat   near   pos   getmenu   instance   run   layout   

E:\Github\hello-world\APP_Addons_0001_Message\app\src\main\java\com\thisway\app_addons_0001_message\MainActivity.java

package com.thisway.app_addons_0001_message;import android.os.Handler;import android.os.Looper;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.Button;import android.view.View;import android.util.Log;import android.os.HandlerThread;public class MainActivity extends AppCompatActivity {    private Button mButton;    private final String TAG="MessageTest";    private int ButtonCount = 0;    private Thread myThread;    private MyThread myThread2;    private Handler mHandler;    private Handler mHandler3;    private int mMessageCount = 0;    private HandlerThread myThread3;    class MyRunnable implements Runnable {        public void run () {            int count = 0;            for (;;) {                Log.d(TAG, "MyThread "+count);                count++;                try {                    Thread.sleep(3000);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    }    class MyThread extends Thread {        private Looper mLooper;        @Override        public void run() {            super.run();            Looper.prepare();            synchronized (this) {                mLooper = Looper.myLooper();                notifyAll();            }            Looper.loop();        }        public Looper getLooper(){            if (!isAlive()) {                return null;            }            // If the thread has been started, wait until the looper has been created.            synchronized (this) {                while (isAlive() && mLooper == null) {                    try {                        wait();                    } catch (InterruptedException e) {                    }                }            }            return mLooper;        }    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mButton = (Button)findViewById(R.id.button);        mButton.setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                // Perform action on click                Log.d(TAG, "Send Message "+ ButtonCount);                ButtonCount++;                Message msg = new Message();                mHandler.sendMessage(msg);                mHandler3.post(new Runnable() {                    @Override                    public void run() {                        Log.d(TAG, "get Message for Thread3 "+ mMessageCount);                        mMessageCount++;                    }                });            }        });        myThread = new Thread(new MyRunnable(), "MessageTestThread");        myThread.start();        myThread2 = new MyThread();        myThread2.start();        mHandler = new Handler(myThread2.getLooper(), new Handler.Callback() {            @Override            public boolean handleMessage(Message msg) {                Log.d(TAG, "get Message "+ mMessageCount);                mMessageCount++;                return false;            }        });        myThread3 = new HandlerThread("MessageTestThread3");        myThread3.start();        mHandler3 = new Handler(myThread3.getLooper());    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, 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();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

 

E:\Github\hello-world\APP_Addons_0001_Message\app\src\main\res\layout\activity_main.xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"    android:orientation="vertical"    >    <Button        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="Send Message"        android:id="@+id/button"        android:layout_gravity="center_horizontal" /></LinearLayout>

 

第3課第1節_基礎知識Android訊息處理機制

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.