Android Sina Weibo client (i)--Main frame building

Source: Internet
Author: User

Source: Miroslav | http://fangjie.sinaapp.com/?p=62 Reprint please indicate the source

The project code has been put into Github:https://github.com/jayfang1993/sinaweibo

In advance, I learned it in accordance with Ivan's tutorial.

First of all, we need to set up a message processing mechanism for any application, that is, when the user clicks on the UI interface. What kind of processing should you do, and then return the results to the user after processing is complete. Displayed on the UI interface. In short: The main framework here is mainly to build up an asynchronous message processing mechanism, the UI thread to send messages, service processing messages. The call UI thread instance update UI interface is finished processing.

Let's take a look at how this is handled, for example:


Parsing: Mainservice is turned on when the program starts . A new thread in the Mainservice constantly loops through the tasks in the task.

When the main thread of the UI interface receives an action from the user. It calls Mainservice.newtask () to put the task into tasks. And put the activity in the activities.

The second thread in the Mainservice will listen to this task, will call Mainservice.dotask (), when finished processing Mainservice the second thread will send handler message to the Mainservice main thread, The processed data is sent to Acitivity, and then the Activity.refresh () is called to refresh the page. It is emphasized here that the activity of the UI layer implements iweiboactivity such an interface. Implement his refresh (), init () two abstract methods.

Mainservice.java

Package Com.fangjie.weibo.logic;import Java.util.arraylist;import Java.util.linkedlist;import java.util.List; Import Java.util.queue;import android.app.activity;import Android.app.service;import Android.content.Intent;import Android.os.handler;import Android.os.ibinder;import Android.os.message;import Android.util.Log;import Com.fangjie.weibo.bean.*;import Com.fangjie.weibo.db.dbuserinfo;import com.fangjie.weibo.ui.IWeiboAcitivity; Import Com.fangjie.weibo.util.getuserinfo;import Com.weibo.sdk.android.oauth2accesstoken;public class MainService Extends Service implements runnable{private static queue<task> tasks=new linkedlist<task> ();p rivate static Arraylist<activity> appactivities=new arraylist<activity> ();p rivate boolean isrun;private Handler handler; @Overridepublic void OnCreate () {//TODO auto-generated method stub/*** open a second thread in Mainservice to process the task * The main is to constantly listen to the tasks stack, from tasks to deal with Task*/isrun=true; Thread thread =new thread (this); Thread.Start (); Super.oncreate ();/*** mainly gets from mAinservice the second thread (dotask) Gets the data processed for the task * and notifies the update UI interface */handler =new handler () {public void Handlemessage (Message msg) { Iweiboacitivity Activity=null;switch (msg.what) {case task.weibo_login:activity= (iweiboacitivity) getActivityByName ("loginactivity"); Activity.refresh (task.weibo_login,msg.obj); Break;case task.get_userinfo_by_token:break;case Task.get_userinfo_in_login:break;default:break;}}};} @Overridepublic void Run () {//TODO auto-generated method Stubwhile (Isrun) {if (!tasks.isempty ()) {Dotask (Tasks.poll ())}}} The/*** UI layer is called to send a task task* UI layer to Mainservice, so static* @param task*/public static void NewTask (Task Task) {Tasks.add (Task);} The/*** UI layer sends tasks to mainservice at the same time. Same to send yourself activity,* in order to call refresh in Mainservice (); * @param task*/public static void addactivty (activity activity) { Appactivities.add (activity);} /*** in the UI layer, the activity is passed to mainservice at the end of the task (refresh). The activity should be removed to prevent confusion!

* @param activity*/public static void removeactivty (activity activity) {appactivities.remove (activity);} /*** the activity instance that was passed when the new task was opened by name * @param name* @return */public activity getactivitybyname (String name) {if (! Appactivities.isempty ()) {for (Activity activity:appactivities) {if (Activity.getclass (). GetName (). INDEXOF (name) >0) {return activity;}}} return null;} /*** handles task* in the tasks stack @param task*/public void Dotask (Task Task) {Message msg=handler.obtainmessage (); msg.what= Task.gettaskid (); switch (Task.gettaskid ()) {//Login operation case TASK.WEIBO_LOGIN:SYSTEM.OUT.PRINTLN ("Login Successful"); msg.obj= "Login Success"; break;//to obtain user information through Access_token and save to database operation case task.get_userinfo_by_token:msg.obj= "success"; break;// Login interface Get user information display operation case task.get_userinfo_in_login:msg.obj= "success"; break;default:break;} Handler.sendmessage (msg);} @Overridepublic ibinder onbind (Intent Intent) {//TODO auto-generated method Stubreturn null;}}


take a look at my project file schema, for example:

Task.java is the entity class for the task, including the ID of the task. The parameter member variable of the task, iweiboactivity is the interface to be implemented by all activity.

 you are welcome to follow my personal website:http://fangjie.sinaapp.com/ 

Android Sina Weibo client (i)--Main frame building

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.