編輯:關於Android編程
復制代碼 代碼如下:
/**
* How to attach an Handler to a Thread:
* If you specify Looper object to Handler, i.e. new Handler(Looper), then the handler is attached to the thread owning
* the Looper object, in which handleMessage() is executed.
* If you do not specify the Looper object, then the handler is attached to the thread calling new Handler(), in which
* handleMessage() is executed.
* In this example, for class CookServer or DeliverServer, if you write this way:
* private class CookServer extends Thread {
private Handler mHandler;
private Looper mLooper;
public CookServer() {
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
....
}
start();
}
* then mHandler is attached to thread calling new CookServer(), which is the main thread, so mHandler.handleMessage() will
* be executed in main thread.
* To attach mHandler to its own thread, you must put it in run(), or after mLooper is created. For our example, providing
* mLooper or not won't matter, because new Handler() is called in run(), which is in a new thread.
*/
public class HandlerITCDemo extends ListActivity {
private static final int COOKING_STARTED = 1;
private static final int COOKING_DONE = 2;
private static final int DELIVERING_STARTED = 3;
private static final int ORDER_DONE = 4;
private ListView mListView;
private static final String[] mFoods = new String[] {
"Cubake",
"Donut",
"Eclaire",
"Gingerbread",
"Honeycomb",
"Ice Cream Sanwitch",
"Jelly Bean",
};
private ArrayList<String> mOrderList;
private TextView mGeneralStatus;
private Button mSubmitOrder;
private static Random mRandomer = new Random(47);
private int mOrderCount;
private int mCookingCount;
private int mDeliveringCount;
private int mDoneCount;
private Handler mMainHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case COOKING_STARTED:
mCookingCount++;
break;
case COOKING_DONE:
mCookingCount--;
break;
case DELIVERING_STARTED:
mDeliveringCount++;
break;
case ORDER_DONE:
mDeliveringCount--;
mDoneCount++;
default:
break;
}
mGeneralStatus.setText(makeStatusLabel());
}
};
private CookServer mCookServer;
private DeliverServer mDeliverServer;
@Override
protected void onDestroy() {
super.onDestroy();
if (mCookServer != null) {
mCookServer.exit();
mCookServer = null;
}
if (mDeliverServer != null) {
mDeliverServer.exit();
mDeliverServer = null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mListView = getListView();
mOrderList = new ArrayList<String>();
mGeneralStatus = new TextView(getApplication());
mGeneralStatus.setText(makeStatusLabel());
mSubmitOrder = new Button(getApplication());
mSubmitOrder.setText("Submit order");
mSubmitOrder.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String order = mFoods[mRandomer.nextInt(mFoods.length)];
mOrderList.add(order);
mOrderCount = mOrderList.size();
mGeneralStatus.setText(makeStatusLabel());
setAdapter();
mCookServer.cook(order);
}
});
mListView.addHeaderView(mGeneralStatus);
mListView.addFooterView(mSubmitOrder);
setAdapter();
mCookServer = new CookServer();
mDeliverServer = new DeliverServer("deliver server");
}
private String makeStatusLabel() {
StringBuilder sb = new StringBuilder();
sb.append("Total: ");
sb.append(mOrderCount);
sb.append(" Cooking: ");
sb.append(mCookingCount);
sb.append(" Delivering: ");
sb.append(mDeliveringCount);
sb.append(" Done: ");
sb.append(mDoneCount);
return sb.toString();
}
private void setAdapter() {
final ListAdapter adapter = new ArrayAdapter<String>(getApplication(), android.R.layout.simple_list_item_1, mOrderList);
setListAdapter(adapter);
}
private class CookServer extends Thread {
private Handler mHandler;
private Looper mLooper;
public CookServer() {
start();
}
@Override
public void run() {
Looper.prepare();
mLooper = Looper.myLooper();
mHandler = new Handler(mLooper, new Handler.Callback() {
public boolean handleMessage(Message msg) {
new Cooker((String) msg.obj);
return true;
}
});
Looper.loop();
}
public void cook(String order) {
if (mLooper == null || mHandler == null) {
return;
}
Message msg = Message.obtain();
msg.obj = order;
mHandler.sendMessage(msg);
}
public void exit() {
if (mLooper != null) {
mLooper.quit();
mHandler = null;
mLooper = null;
}
}
}
private class Cooker extends Thread {
private String order;
public Cooker(String order) {
this.order = order;
start();
}
@Override
public void run() {
mMainHandler.sendEmptyMessage(COOKING_STARTED);
SystemClock.sleep(mRandomer.nextInt(50000));
mDeliverServer.deliver(order);
mMainHandler.sendEmptyMessage(COOKING_DONE);
}
}
private class DeliverServer extends HandlerThread {
private Handler mHandler;
public DeliverServer(String name) {
super(name);
start();
}
@Override
protected void onLooperPrepared() {
super.onLooperPrepared();
mHandler = new Handler(getLooper(), new Handler.Callback() {
public boolean handleMessage(Message msg) {
new Deliver((String) msg.obj);
return true;
}
});
}
public void deliver(String order) {
if (mHandler == null || getLooper() == null) {
return;
}
Message msg = Message.obtain();
msg.obj = order;
mHandler.sendMessage(msg);
}
public void exit() {
quit();
mHandler = null;
}
}
private class Deliver extends Thread {
private String order;
public Deliver(String order) {
this.order = order;
start();
}
@Override
public void run() {
mMainHandler.sendEmptyMessage(DELIVERING_STARTED);
SystemClock.sleep(mRandomer.nextInt(50000));
mMainHandler.sendEmptyMessage(ORDER_DONE);
}
}
}
前言:俗話說磨刀不誤砍柴工,一個優秀的產品從一個不錯的點子直到用戶的手中,是需要一個團隊不遺余力協同合作不斷打磨出來的;同樣,一個好的App除正常的代碼編寫外,還需要經過
ViewPager做導航想不想有這樣的效果: 比如說有四張圖片,下面有四個圓點,當頁面滑動的時候一個點變大一個點變小(或者是一個點變小一個點變大),等於說同時在執行兩個動
如果移動端訪問不佳或需要更好的閱讀體驗,歡迎使用 ==> Github 版使用 RadioGroup 和 ViewPager 實現更加可定制的效果。背景昨天設計圖剛
一、前言對於APK裡面的Resources.arsc文件大家應該都知道是干什麼的,它實際上就是App的資源索引表。下面我會結合實例對它的格式做一下剖析,讀完這篇文章應該能