`

Foursquare 源码研究之------登录续

阅读更多

上次说到了登录回调方法onPostExecute,此方法是AsyncTask的方法,当doInBackground方法执行完后调用此方法来处理返回结果.在onPostExecute中主要根据登录结果来做不同的事情,登录成功://发送登录广播

sendBroadcast(new Intent(Foursquared.INTENT_ACTION_LOGGED_IN)); 

此广播的订阅时在Foursquared类中实现的,首先看此类Foursquared extends Application,Application是android项目运行的一个全局的状态 "Base class for those who need to maintain global application state". 可以在Manifest.xml中指定你自定义的application. 在此类中有一个LoggedInOutBroadcastReceiver 来处理接收到的广播,然后广播发送一个message.此message对应有一个TaskHandler.在foursquare中他使用了

mTaskThread = new HandlerThread(TAG + "-AsyncThread");
mTaskThread.start();
mTaskHandler = new TaskHandler(mTaskThread.getLooper());

 上面的方式来创建Handler. 是为了让handler运行在新的线程中. 接下来看handleMessage方法对msg的处理.第一个switch case语句是MESSAGE_UPDATE_USER, 更新一下用户的信息.同时保存在SharedPreferenses中.

 

接下来开始一个更新UI的Service  foursquared.requestStartService(); 主要调用FoursquaredService类的updateWidgets方法来更新widget.也是在Foursquared类中来进行更新,看handleMessage():

 case MESSAGE_START_SERVICE: //updateWidgets更新组件
    Intent serviceIntent = new Intent(Foursquared.this, FoursquaredService.class);
      serviceIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
      startService(serviceIntent);
      return;

 启动一个FoursquaredService.

 

接下来返回到主界面 

 // Launch the main activity to let the user do anything.
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);

 如果登录失败:

sendBroadcast(new Intent(Foursquared.INTENT_ACTION_LOGGED_OUT));//发送登出广播.
NotificationsUtil.ToastReasonForFailure(LoginActivity.this, mReason);//根据不同的exception来提示失败信息

 最后关闭进度条dismissProgressDialog().

到这里登录就结束了.写的比较乱.呵呵.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics