【第3课第1节_基础知识Android消息处理机制】不操千曲而后晓声,观千剑而后识器。这篇文章主要讲述第3课第1节_基础知识Android消息处理机制相关的知识,希望能为你提供帮助。
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>
推荐阅读
- Android国际化设置
- android studio 配置 Genymotion 以及Genymotion安装配置等
- Android 常见对话框
- App架构设计经验谈:接口的设计
- 使用Failwith关键字的F#抛出异常
- 5大最佳SDHC卡恢复软件合集(不能错过的工具!)
- 10大最佳免费照片恢复软件合集(哪个最适合你())
- 10大最佳SD卡修复工具合集(哪个最适合你())
- 15大最佳SD卡恢复软件合集(不能错过的工具!)