foursquare -6 Handler

 
        // Sometimes we want the application to do some work on behalf of the
        // Activity. Lets do that
        // asynchronously.
        mTaskThread = new HandlerThread(TAG + "-AsyncThread"); //初始化LOOP, run 方法中prepare(),调用LOOP 的 loop()
        //Loop  循环分发处理消息 
        mTaskThread.start();
        mTaskHandler = new TaskHandler(mTaskThread.getLooper());


handler 定义

    private class TaskHandler extends Handler {

        private static final int MESSAGE_UPDATE_USER = 1;
        private static final int MESSAGE_START_SERVICE = 2;

        public TaskHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {//消息处理
            super.handleMessage(msg);
            if (DEBUG) Log.d(TAG, "handleMessage: " + msg.what);

            switch (msg.what) {
                case MESSAGE_UPDATE_USER:
                    ............................
                    return;

                case MESSAGE_START_SERVICE:
                    Intent serviceIntent = new Intent(Foursquared.this, FoursquaredService.class);
                    serviceIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
                    startService(serviceIntent);
                    return;
            }
        }
    }


调用方法

    public void requestStartService() {
        mTaskHandler.sendMessage( //把消息放到队列中
                mTaskHandler.obtainMessage(TaskHandler.MESSAGE_START_SERVICE) );//生成消息
    }

    public void requestUpdateUser() {
        mTaskHandler.sendEmptyMessage(TaskHandler.MESSAGE_UPDATE_USER);
    }


 

你可能感兴趣的:(service,user,application,Class)