編輯:關於Android編程
因為最大部分的service不需要同時處理多個請求(處理多個請求是一個比較危險的多線程的場景),這樣在在這種情況下呢,最好使用IntentService類如果你實現你的服務。
使用intentService與service有什麼不同呢
(1)直接 創建一個默認的工作線程,該線程執行所有的intent傳遞給onStartCommand()區別於應用程序的主線程。
(2)直接創建一個工作隊列,將一個意圖傳遞給你onHandleIntent()的實現,所以我們就永遠不必擔心多線程。
(3)當請求完成後自己會調用stopSelf(),所以你就不用調用該方法了。
(4)提供的默認實現onBind()返回null,所以也不需要重寫這個方法。so easy啊
(5)提供了一個默認實現onStartCommand(),將意圖工作隊列,然後發送到你onHandleIntent()實現。真是太方便了
我們需要做的就是實現onHandlerIntent()方法,還有一點就是經常被遺忘的,構造函數是必需的,而且必須調用超IntentService(字符串) ,因為工作線程的構造函數必須使用一個名稱。如何實現呢,我們借助於谷歌官方文檔來看一下吧。
public class HelloIntentService extends IntentService { /** * A constructor is required, and must call the super IntentService(String) * constructor with a name for the worker thread. */ public HelloIntentService() { super("HelloIntentService"); } /** * The IntentService calls this method from the default worker thread with * the intent that started the service. When this method returns, IntentService * stops the service, as appropriate. */ @Override protected void onHandleIntent(Intent intent) { // Normally we would do some work here, like download a file. // For our sample, we just sleep for 5 seconds. long endTime = System.currentTimeMillis() + 5*1000; while (System.currentTimeMillis() < endTime) { synchronized (this) { try { wait(endTime - System.currentTimeMillis()); } catch (Exception e) { } } } } }
public abstract class IntentService extends Service { private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; private String mName; private boolean mRedelivery; private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); stopSelf(msg.arg1); } } /** * Creates an IntentService. Invoked by your subclass's constructor. * * @param name Used to name the worker thread, important only for debugging. */ public IntentService(String name) { super(); mName = name; } /** * Sets intent redelivery preferences. Usually called from the constructor * with your preferred semantics. * *我們可以看到源代碼裡頭的第15行handlerMessage方法裡當處理完請求後就會調用stopself()方法了,外界就不用調用了,此外還有一點我們可以看到代碼最後一行第110行,onhandleIntent()是一個抽象類,而其他類都是抽象類,所以我們就可以理解為什麼只需要重寫onhandleIntent()方法了吧。If enabled is true, * {@link #onStartCommand(Intent, int, int)} will return * {@link Service#START_REDELIVER_INTENT}, so if this process dies before * {@link #onHandleIntent(Intent)} returns, the process will be restarted * and the intent redelivered. If multiple Intents have been sent, only * the most recent one is guaranteed to be redelivered. * *
If enabled is false (the default), * {@link #onStartCommand(Intent, int, int)} will return * {@link Service#START_NOT_STICKY}, and if the process dies, the Intent * dies along with it. */ public void setIntentRedelivery(boolean enabled) { mRedelivery = enabled; } @Override public void onCreate() { // TODO: It would be nice to have an option to hold a partial wakelock // during processing, and to have a static startService(Context, Intent) // method that would launch the service & hand off a wakelock. super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.start(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); } @Override public void onStart(Intent intent, int startId) { Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); } /** * You should not override this method for your IntentService. Instead, * override {@link #onHandleIntent}, which the system calls when the IntentService * receives a start request. * @see android.app.Service#onStartCommand */ @Override public int onStartCommand(Intent intent, int flags, int startId) { onStart(intent, startId); return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY; } @Override public void onDestroy() { mServiceLooper.quit(); } /** * Unless you provide binding for your service, you don't need to implement this * method, because the default implementation returns null. * @see android.app.Service#onBind */ @Override public IBinder onBind(Intent intent) { return null; } /** * This method is invoked on the worker thread with a request to process. * Only one Intent is processed at a time, but the processing happens on a * worker thread that runs independently from other application logic. * So, if this code takes a long time, it will hold up other requests to * the same IntentService, but it will not hold up anything else. * When all requests have been handled, the IntentService stops itself, * so you should not call {@link #stopSelf}. * * @param intent The value passed to {@link * android.content.Context#startService(Intent)}. */ protected abstract void onHandleIntent(Intent intent); }
之前實現過了天氣預報的功能 但是真的好丑 真的只是實現功能 所以上一篇博客也沒有貼出圖片 這次 相對於第一個 首先是界面做了調整 其次就是 之前那個只能查看實時天氣 這個
實現原理通過利用AccessibilityService輔助服務,監測屏幕內容,如監聽狀態欄的信息,屏幕跳轉等,以此來實現自動拆紅包的功能。關於Accessibility
東東手游助手是款官方推出的安卓模擬器,在東東手游助手運行時出現閃退這種異常現象是因為什麼呢?怎麼解決東東手游助手閃退問題呢?先分析東東手游助手閃退原因:1.
生成apk最懶惰的方法是:只要你運行過android項目,到工作目錄的bin文件夾下就能找到與項目同名的apk文件,這種apk默認是已經使用debug用戶簽名的。如果想要