码迷,mamicode.com
首页 > 移动开发 > 详细

androidannotations的background和UiThread配合使用參考

时间:2017-05-14 21:45:03      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:result   介绍   思考   roi   rup   androi   context   cep   block   

简单介绍

androidannotations在开发中的代码规范思考:(MVC思考)时间太紧,先贴代码:

Activity的代码:
package edu.njupt.zhb.main;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;

import android.app.Activity;

/**
 * @author Zheng Haibo
 * @web  http://www.mobctrl.net
 */
@EActivity(R.layout.main)
public class MainActivity extends Activity {

	@Bean
	MainActions mainActions;

	@AfterViews
	void afterViews() {
		doSomething();
	}

	private void doSomething() {
		mainActions.doActionsInThread(20, new UiCallback() {

			@Override
			public void onBackgroundEnd() {
				// TODO Auto-generated method stub
				// write the code
				System.out.println("the background thread end...");
			}
		});
	}
}

业务逻辑
MainActions
package edu.njupt.zhb.main;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;
import org.androidannotations.annotations.UiThread;

import android.content.Context;
import android.widget.Toast;
/**
 * @author Zheng Haibo
 * @web  http://www.mobctrl.net
 */
@EBean
public class MainActions {

	@RootContext
	Context context;

	@Background
	void doActionsInThread(int params, UiCallback uiCallback) {
		// database,net,file,sp ...
		int result = 0;
		for (int i = 0; i < params; i++) {
			result = result + i;
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("test the thread is run in the background...");
		}
		runInUiThread(result, uiCallback);
	}

	@UiThread
	void runInUiThread(int result, UiCallback uiCallback) {
		Toast.makeText(context, result + ", just a test", Toast.LENGTH_SHORT)
				.show();
		System.out.println("you can write the ui code ...");
		uiCallback.onBackgroundEnd();
	}
}

回调接口

package edu.njupt.zhb.main;

/**
 * @author Zheng Haibo
 * @web http://www.mobctrl.net
 */
public interface UiCallback {
	public void onBackgroundEnd();
	// ...
}

我认为依照这个思路写。代码会比較清爽。

兴许说原因,先回宿舍了。

。。。

androidannotations的background和UiThread配合使用參考

标签:result   介绍   思考   roi   rup   androi   context   cep   block   

原文地址:http://www.cnblogs.com/mfmdaoyou/p/6853664.html

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