http://www.tuicool.com/articles/aaaiae html
http://androidannotations.org/ java
@Retention(RetentionPolicy.CLASS) @Target({java.lang.annotation.ElementType.TYPE}) public @interface EActivity{ public abstract int value(); public abstract String resName(); }
利用Java Annotation Processing Tool (APT) 在編譯源文件(*.java)以前,經過一個自定義的註解處理器(AnnotationProcessor)解釋並處理源文件中的註解,由註解處理器生成 一些新的源文件,字節碼文件(*.class),或其餘的文本文件,APT也會對新生成源文件進行編譯,直到沒有新的文件生成。
編譯前: android
package com.some.company; @EActivity public class MyActivity extends Activity { // ... }編譯後:
package com.some.company; public final class MyActivity_ extends MyActivity { // ... }
因此 AndroidManifest.xml中用到的Activity和Application都要加下劃線:xxActivity_,xxApplication_
git
startActivity(this, MyListActivity_.class);
startService(this, MyService_.class);
package com.googlecode.androidannotations.helloworldeclipse; @EActivity(R.layout.my_activity) //佈局文件在這裏聲明,不用在setContentView public class MyActivity extends Activity { @ViewById //控件這樣標註,因爲是IOC模式,所以不須要本身實例化 EditText myEditText; @ViewById(R.id.myTextView) //提供id來生成控件,若是不指定ID,默認以控件名進行查找,如上面的myEditText TextView textView; @StringRes(R.string.hello) //資源,若是字段名和資源命名一直能夠省略,以下 String helloFormat; @ColorRes int androidColor; @BooleanRes boolean someBoolean; @SystemService NotificationManager notificationManager; @SystemService WindowManager windowManager; public void onBackPressed() { Toast.makeText(this, "Back key pressed!", Toast.LENGTH_SHORT).show(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // windowManager should not be null windowManager.getDefaultDisplay(); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); } @Click //事件控制,能夠以按鈕的id做爲方法名,同時支持的事件還有onLongClick,onTextChange等 void myButtonClicked() { String name = myEditText.getText().toString(); setProgressBarIndeterminateVisibility(true); someBackgroundWork(name, 5); } @Background //開啓新線程後臺運行,注意不要引用UI控件,並且返回值類型必定是void void someBackgroundWork(String name, long timeToDoSomeLongComputation) { try { TimeUnit.SECONDS.sleep(timeToDoSomeLongComputation); } catch (InterruptedException e) { } String message = String.format(helloFormat, name); updateUi(message, androidColor); showNotificationsDelayed(); } @UiThread //UI線程 void updateUi(String message, int color) { setProgressBarIndeterminateVisibility(false); textView.setText(message); textView.setTextColor(color); } @UiThread(delay=2000) //能夠設置延時時間,以毫秒爲單位 void showNotificationsDelayed() { Notification notification = new Notification(R.drawable.icon, "Hello !", 0); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0); notification.setLatestEventInfo(getApplicationContext(), "My notification", "Hello World!", contentIntent); notificationManager.notify(1, notification); } @LongClick void startExtraActivity() { Intent intent = new Intent(this, ActivityWithExtra_.class); intent.putExtra(ActivityWithExtra.MY_DATE_EXTRA, new Date()); intent.putExtra(ActivityWithExtra.MY_STRING_EXTRA, "hello !"); intent.putExtra(ActivityWithExtra.MY_INT_EXTRA, 42); startActivity(intent); } @Click void startListActivity(View v) { startActivity(new Intent(this, MyListActivity_.class)); } @Touch void myTextView(MotionEvent event) { Log.d("MyActivity", "myTextView was touched!"); } @Transactional int transactionalMethod(SQLiteDatabase db, int someParam) { //dosomething; } }
使用AndroidAnnotations好處就在於使得代碼量小並且簡潔,使咱們更多的關注於業務邏輯而不是頁面 github
一些經常使用註釋的使用方法:
@AfterInject定義的方法在類的構造方法執行後執行
@AfterTextChange定義的方法在TextView及其子類的Text屬性改變後執行
@AfterViews定義的方法在setContentView後執行
@Background定義的方法在後臺線程執行,開啓的線程在線程池中管理
@BeforeTextChange定義的方法在TextView及其子類的Text屬性改變前執行
@Click定義點擊監聽器
@EActivity在Activity中啓用Annotations
@EProvider在 ContentProvider中啓用Annotations
@EReceive在BroadcastReceiver中啓用Annotations
@EService在Service中啓用Annotations
@EView在自定義的View的子類中啓用Annotations
@Fullscreen全屏 網絡
@NoTitle 無標題欄 session
更多註解的詳細用法參見官方網上 Cookbook 及全部 可用的 annotations 列表文檔
優勢:1,AndroidAnnotations框架支持@Rest標籤,引入方便,導入jar包便可
2,Rest支持多種返回類型解析:Json,xml,String,ByteArray,Form,Source,自定義轉換器
3,多種請求方式,能夠自定義頭信息,能夠對請求進行受權
缺點:1,屢次請求沒法共享session(能夠經過傳入自定義的httpclient解決)
2,沒法集中處理返回的Msg對象(統一處理TimeOut和一些異常信息),除非修改源碼
3,沒法控制中止已發起的請求
4,須要引入多個jar包,Spring-android-rest-template.jar,Spring-android-core.jar,
Spring-android-auth.jar和jackson-all.jar
我的認爲仍是不要用Rest的這套東西,網絡請求仍是咱們本身寫比較合適 oracle
1,onSaveInstanceState的使用
保存activity暫時的狀態(保存類成員變量關聯着UI的值,例如:某個處於選中狀態下RadioButton的ID),
便於恢復
2,使用@EBean的JavaBean,能直接取到調用他的Context,而且支持@ViewById標籤找到控件,所以,
構造方法傳過來的參數能夠少不少。
例如:
框架
@EBean public class Student { //RootContext能取到調用該Bean的Context,構造方法再也不須要傳Context參數 @RootContext Context context; @RootContext MainActivity activity; //ViewById也能在這裏直接使用 @ViewById TextView tv; public void Toast(){ Toast.makeText(context, "在Ebean中調用", Toast.LENGTH_LONG).show(); } //後臺線程執行 @Background public void backThread(){ //dosomething activity.updateTv(i); //更新UI,調用在UI線程執行的方法 updateTv(i); } //UiThread在UI線程執行 @UiThread public void updateTv(int i){ tv.setText(String.valueOf(i)); } //AfterInject在構造方法執行完成後執行 @AfterInject public void doSomethingAfterInject(){ System.out.println("Student AfterInject"); } }