不定期更新,無廢話開始:List<Integer>不是List<Number>的子類,但List<Integer>是Collection<Integer>的子類。
- 一個List<? extends Number>的對象引用可以被賦值任何以Number或其子類為泛型的List或其子類的對象實例引用,如下例
1 List<? extends Number> nums = new ArrayList<Integer>();
- 泛型方法可以顯示指定其返回結果的泛型類型,如下例
1 Arrays.<Object>asList(1, 1.2, "Hello");
2 Collections.<Number>copy(objs, ints);
- Get與Put原則:當僅從結構中取出值則使用extends通配符,當僅向結構中添加值則使用super通配符,當對一個結構即取出又添加值時不要使用任何通配符。
1 //Collections.copy方法即使用super又使用extends的例子
2 public static <T> void copy(List<? super T> dest, List<? extends T> src)
1 //另一個例子,對同一個結構即取值又設值,不使用任何通配符
2 public static double sum(Collection<? extends Number> nums) {
3 double s = 0.0;
4 for (Number num : nums) s += num.doubleValue();
5 return s;
6 }
7
8 public static void count(Collection<? super Integer> ints, int n) {
9 for (int i = 0; i < n; i++) ints.add(i);
10 }
11
12 //注意這個方法參數的簽名,沒有使用任何通配符
13 public static double sumCount(Collection<Number> nums, int n) {
14 count(nums, n);
15 return sum(nums);
16 }
啟動應用
onCreate
onStart
onResume
按退出鍵
onPause
onStop
onDestory
按HOME鍵或當前應用被其他全屏應用覆蓋時(如電話呼入)
onPause
onStop
按HOME鍵后,再次點擊應用圖標或長按HOME鍵點擊任務列表中的應用圖標或覆蓋當前應用的其他全屏應用退出后
onRestart
onStart
onResume
被彈出對話框覆蓋時(如彈出短信提醒對話框)或屏幕關閉時
onPause
對話框關閉時或屏幕點亮時
onResume
橫豎屏切換時
onPause
onStop
onDestory
onCreate
onStart
onResume
ImageView中有setImageURI,但是傳遞一個URI.parse("http://www.google.com.hk/tools/dlpage/res/chrome/images/chrome-205_noshadow.png")這樣的URL路徑不能成功載入網絡上的圖片,最終還是需要借助java.net.URL,如下代碼所示:
1 URL picUrl = new URL("http://www.google.com.hk/tools/dlpage/res/chrome/images/chrome-205_noshadow.png");
2
3 Bitmap pngBM = BitmapFactory.decodeStream(picUrl.openStream());
4
5 imageView.setImageBitmap(pngBM);
很簡單吧,哪位朋友如果有更簡單的方法請通知我。
懶得再翻譯,這段應該很好理解,直接將Dev Guide中復制過來。
The SharedPreferences
class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences
to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).
To get a SharedPreferences
object for your application, use one of two methods:
getSharedPreferences()
- Use this if you need multiple preferences files identified by name, which you specify with the first parameter.
getPreferences()
- Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.
To write values:
- Call
edit()
to get a SharedPreferences.Editor
.
- Add values with methods such as
putBoolean()
and putString()
.
- Commit the new values with
commit()
To read values, use SharedPreferences
methods such as getBoolean()
and getString()
.
Here is an example that saves a preference for silent keypress mode in a calculator:
1
public class Calc extends Activity
{
2
public static final String PREFS_NAME = "MyPrefsFile";
3
4
@Override
5
protected void onCreate(Bundle state)
{
6
super.onCreate(state);
7
. . .
8
9
// Restore preferences
10
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
11
boolean silent = settings.getBoolean("silentMode", false);
12
setSilent(silent);
13
}
14
15
@Override
16
protected void onStop()
{
17
super.onStop();
18
19
// We need an Editor object to make preference changes.
20
// All objects are from android.context.Context
21
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
22
SharedPreferences.Editor editor = settings.edit();
23
editor.putBoolean("silentMode", mSilentMode);
24
25
// Commit the edits!
26
editor.commit();
27
}
28
}
首先內部存儲路徑為/data/data/youPackageName/,下面講解的各路徑都是基于你自己的應用的內部存儲路徑下。所有內部存儲中保存的文件在用戶卸載應用的時候會被刪除。
一、 files
1. Context.getFilesDir(),該方法返回/data/data/youPackageName/files的File對象。
2. Context.openFileInput()與Context.openFileOutput(),只能讀取和寫入files下的文件,返回的是FileInputStream和FileOutputStream對象。
3. Context.fileList(),返回files下所有的文件名,返回的是String[]對象。
4. Context.deleteFile(String),刪除files下指定名稱的文件。
二、cache
1. Context.getCacheDir(),該方法返回/data/data/youPackageName/cache的File對象。
三、custom dir
getDir(String name, int mode),返回/data/data/youPackageName/下的指定名稱的文件夾File對象,如果該文件夾不存在則用指定名稱創建一個新的文件夾。