码迷,mamicode.com
首页 > 其他好文 > 详细

AsyncHandler

时间:2014-08-09 15:55:18      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:des   android   java   os   io   for   art   ar   

package com.ango.deskclock;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;

/**
 * Helper class for managing the background thread used to perform io operations
 * and handle async broadcasts.
 */
final class AsyncHandler {

    private static final HandlerThread sHandlerThread =
            new HandlerThread("AsyncHandler");
    private static final Handler sHandler;
    private static Runnable aRunnable;

    static {
        sHandlerThread.start();
        sHandler = new Handler(sHandlerThread.getLooper());
    }

    public static void post(Runnable r) {
        sHandler.post(r);
    }
    
    public static final boolean postDelayed(Context context, Alarm alarm, long delayMillis) {
    	final Context sContext = context;
    	final Alarm sAlarm = alarm;
    	aRunnable = new Runnable() {
            @Override public void run() {
                Intent playAlarm = new Intent(Alarms.ALARM_ALERT_ACTION);
                playAlarm.putExtra(Alarms.ALARM_INTENT_EXTRA, sAlarm);
                sContext.startService(playAlarm);
              }
    	};
    	return sHandler.postDelayed(aRunnable, delayMillis);
    }
    
    public static final void removeCallbacks (){
    	sHandler.removeCallbacks(aRunnable);
    }

    private AsyncHandler() {}
}

        AsyncHandler.post(new Runnable() {
            @Override public void run() {
                handleIntent(context, intent);
                result.finish();
                wl.release();
            }
        });


//        // Play the alarm alert and vibrate the device after 10 seconds so that the user can dismiss or snooze before the alarm alert.
//        Intent playAlarm = new Intent(Alarms.ALARM_ALERT_ACTION);
//        playAlarm.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm);
//        context.startService(playAlarm);
        AsyncHandler.postDelayed(context, alarm, 10000); 

        AsyncHandler.removeCallbacks();  // remove the AsyncHandler.postDelayed(context, alarm, 10000) in AlarmReceiver.java






AsyncHandler,布布扣,bubuko.com

AsyncHandler

标签:des   android   java   os   io   for   art   ar   

原文地址:http://blog.csdn.net/llrraa2010/article/details/38456921

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!