編輯:Android開發教程
前面介紹了RogoGuice2.0的基本用法,其它使用可以參見RoboGuice1.1開發 指南,2.0中提供了對Fragment,View(自定義View中使用注入)的支持,本博 客不再一一介紹。
本例使用的是RoboGuice 開發包中的簡單示例 Astroboy(阿童木)。涉及的使用RoboGuice2.0 的一些常用方法。
本例 下載(Eclipse項目)。
下面對項目中RoboGuice2的使用進行解釋。因為 本例沒使用自定義綁定,所以無需使用res/values/roboguice.xml 定義Module. 如有自定義模塊,可以參見Android RoboGuice2 使用指南(2): 第一個例子 Hello World。
1. 類Astroboy
// There's only one Astroboy, so make it a @Singleton.
// This means that there will be only one instance of Astroboy in the
entire
// app.
// Any class that requires an instance of Astroboy will get the same
instance.
// This also means this class needs to be thread safe, of course
@Singleton
public class Astroboy {
// Because Astroboy is a Singleton, we can't directly inject the
current
// Context since the current context may change depending on what
activity
// is using Astroboy
// at the time. Instead we use the application context.
// Vibrator is bound to context.getSystemService(VIBRATOR_SERVICE)
in
// DefaultRoboModule.
// Random has no special bindings, so Guice will create a new
instance for
// us.
@Inject Application application;
@Inject Vibrator vibrator;
@Inject Random random;
public void say(String something) {
// Make a Toast, using the current context as returned by the
Context
// Provider
Toast.makeText(application, "Astroboy says, \"" + something +
"\"",
Toast.LENGTH_LONG).show();
}
public void brushTeeth() {
vibrator.vibrate(
new long[] { 0, 200, 50, 200, 50, 200, 50, 200, 50,
200, 50,
200, 50, 200, 50, 200, 50, 200, 50, 200, 50,
200, 50, },
-1);
}
public String punch() {
final String expletives[] = new String[] { "POW!", "BANG!",
"KERPOW!",
"OOF!" };
return expletives[random.nextInt(expletives.length)];
}
}
程序中只希望使用一個Astroboy實例,因此可以使用@Singleton標注,此後 任何使用
@Inject Astroboy astroboy;
注入的Astroboy都會指向 同一個實例,這也是符合Singleton設計模式的。
@Inject Application application; 注入Application實例。參見Android RoboGuice 使用指南 (15):Inject Context
@Inject Vibrator vibrator; 注入Android Vibrator實例,參見Android RoboGuice 使用指南(16):Standard Injection
@Inject Random random; 對於普通的Java 類型(POJO),如 果該類具有缺省構造函數(不帶參數的等),也可以使用RoboGuice自動注入實 例。
因此當Astroboy創建時,RoboGuice 自動為application, vibrator, random 創建實例,無需使用new 或參數傳入來構造它們。
2. 類AstroboyRemoteControl
/**
* A class to control Astroboy remotely.
*
* This class uses the current context, so we must make it
@ContextSingleton.
* This means that there will be one AstroboyRemoteControl for every
activity or
* service that requires one. Note that we actually ask for the
Activity, rather
* than the Context (which is the same thing), because we need access
to some
* activity-related methods and this saves us from having to downcast
to an
* Activity manually.
*
* It also asks RoboGuice to inject the Astroboy instance so we can
control him.
*
* What you'll learn in this class - What @ContextScope means and when
to use it
* - How to inject an Activity instead of a Context (which is really
the same
* thing) - How to use RoboGuice's convenient and flexible logging
facility, Ln.
*/
@ContextSingleton
public class AstroboyRemoteControl {
// The Astroboy class has been decorated with @Singleton, so this
instance
// of Astroboy will be the same instance used elsewhere in our
app.
// Injecting an Activity is basically equivalent to "@Inject
Context context",
// and thus also requires @ContextScope. If you wanted, you could
also
// @Inject Application, Service, etc. wherever appropriate.
@Inject Astroboy astroboy;
@Inject Activity activity;
public void brushTeeth() {
// More info about logging available here:
// http://code.google.com/p/roboguice/wiki/Logging
Ln.d("Sent brushTeeth command to Astroboy");
astroboy.brushTeeth();
}
public void say(String something) {
Ln.d("Sent say(%s) command to Astroboy", something);
astroboy.say(something);
}
public void selfDestruct() {
Toast.makeText(
activity,
"Your evil remote control has exploded! Now Astroboy
is FREEEEEEEEEE!",
Toast.LENGTH_LONG).show();
activity.finish();
}
}
與Singleton類似的一個Scope標注為@ContextSingleton ,它表示對 於每個Activity實例有一個實例,不同的activity對應不同的實例。
@Inject Astroboy astroboy; 注入同一個Astroboy實例(Singleton) 。
@Inject Astroboy astroboy; 注入對應的Activity實例。
Android 4.4急需的新功能
Google馬上就要正式發布Android 4.4 KitKat了。據我們現在所知,一些新的用戶界面預示著這次的操作系統將有一些新的改變,比如說重新設計的電話應用,比如說
Android命令行截屏screencap
Android下面使用命令行截圖。因為工作調試用的機器,沒法連接USB,所以用不了一般的截圖方法,後來查了一下,Android4.0以後都內置了截圖命令。可以使用下面命令
Android LibGDX游戲引擎開發教程(七) 中文字體的顯示和繪制(上)
在字體的顯示和繪制中,Libgdx的作者(Mario Zechner,美國人)給我們提供了一個非常好用的工具——Hiero,那麼下面就來看看它具體
Android開發入門(十九)數據庫 19.3 預創建數據庫
在開發過程中,有時候,預先創建好數據庫比在程序運行時創建數據庫更加地高效。舉個例子,你想編寫 一個程序,這個程序把你去過的地方的坐標都顯示出來。這種情況下,預先創建數據庫
Android ApiDemos示例解析(43):App->Service->Remote Service Controller
Remote Service Controller 和使用Local S