您的位置:首页 > 移动开发 > Android开发

Android异步消息处理实例

2017-03-30 17:32 155 查看
憋说话,看代码

package xxxxxx

import android.os.HandlerThread;
import android.os.Looper;
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.Handler;;
import android.os.Message;

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;
public void run(){
super.run();
Looper.prepare();
synchronized (this){
mLooper = Looper.myLooper();
notifyAll();
}
Looper.loop();
}

public Looper getLooper(){
if(!isAlive()){
return null;
}

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(){
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 异步