編輯:關於Android編程
public class HandlerThread extends Thread { private int mPriority; private int mTid =-1; private Looper mLooper; publicHandlerThread(String name) { super(name); mPriority =Process.THREAD_PRIORITY_DEFAULT; } publicHandlerThread(String name, int priority) { super(name); mPriority =priority; } protected void onLooperPrepared() { } public void run() { mTid =Process.myTid(); Looper.prepare(); synchronized(this) { mLooper =Looper.myLooper(); notifyAll(); } Process.setThreadPriority(mPriority); onLooperPrepared(); Looper.loop(); mTid = -1; } public Looper getLooper() { if (!isAlive()) { return null; } // If the threadhas been started, wait until the looper has been created. synchronized(this) { while(isAlive() && mLooper == null) { try { wait(); } catch(InterruptedException e) { } } } return mLooper; } public boolean quit(){ Looper looper =getLooper(); if (looper !=null) { looper.quit(); return true; } return false; } public intgetThreadId() { return mTid; } }此類就是繼承了Thread類,使用此類時一定要注意必須start(),否則run()方法沒有調用,handler機制也就沒有建立起來。
public class BackService extends Service { private ServiceHandler serviceHandler; @Override public IBinder onBind(Intent arg0) { return null; } private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { super.handleMessage(msg); onHandleIntent((Intent) msg.obj); // 在其參數startId跟最後啟動該service時生成的ID相等時才會執行停止服務。 stopSelf(msg.arg1); } } @Override public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread("BackService"); thread.start(); Looper serviceLooper = thread.getLooper(); serviceHandler = new ServiceHandler(serviceLooper); } @Override public void onStart(Intent intent, int startId) { Message msg = serviceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; serviceHandler.sendMessage(msg); } protected void onHandleIntent(Intent intent) { //做你的異步任務 } }
一、源代碼源代碼及demo二、背景先看看Win10的加載動畫(找了很久才找到):每次打開電腦都會有這個加載動畫,看上挺cool的,就想著自己能否實現它。要實現這個動畫?首
我們要實現的目標是:做一個短信發送器 界面: 因為要涉及到短信發送這種屬於隱私的問題,所以我們要在AndroidManifest.xml中添加一行代碼,來獲得權限: 然
魅族的魅藍手機通常都是只有移動和聯通版兩種版本的,但是最近最新發布的魅藍E再次吧魅藍手機推向了一個新的高度,很多機友就開始問小編了。魅族魅藍E支持什麼SIM
前言:上次呢為大家介紹了如何給RecycleView添加頭部、尾部達到裝飾的效果,那麼本章節將為大家介紹RecycleView實現不一樣的拖動排序以及側滑刪除。首先呢我們