package com.linxiaosheng.handler_02;
import javax.security.auth.callback.Callback;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private TextView textView;
private Handler handler=new Handler(new android.os.Handler.Callback(){
@Override
public boolean handleMessage(Message msg) {
// TODO Auto-generated method stub
System.out.println(msg.arg1);
//这里返回false则会调回handler的handlerMessage函数
//返回true则不会调用
//所以可以先做拦截判断
return false;
}
} ){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
System.out.println(msg.arg1);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
textView=(TextView)findViewById(R.id.textview);
//创建一个线程
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
Thread.sleep(1000);
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
textView.setText("update textview..");
Message message=new Message();
message.arg1=88;
handler.sendMessage(message);
}
});
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}