Android note (5/8 update)

Just a note.

§Activity launch modes
<activity android:name=".HelloWrold"
android:label="@string/app_name"
android:launchMode="singleTask">

<activity android:configChanges="orientation">
Reference: Activity | Android Developer


§Android SDK 1.5和2.2 讀寫文件的差別

在進行對sdcard下檔案的讀寫操作的時候
SDK 1.5 正常,但是改用SDK 2.2之後直接爆炸

後來上網google了一下 1.5的和2.2的對檔案的讀寫不同的

在SDK 1.5
FleOutputStream file = new FileOuptutStream("/sdcard/test.txt");
在SDK 1.5中 如果test.txt不存在的話 系統會自動的為你創建這個文件
但是到了SDK 2.2 你必須...
File file = new File ("/sdcard/test.txt");
if(!file.exist()) file.createNewFile();
FileOutputStream file_out = new FileOutputStream("file");

並且還得在AndroidMainifest.xml中添加如下的權限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Reference: Android2.2和Android1.5讀寫文件的差別


§Android R Drawables
android.R.drawable.*


§NotificationManager & Notification
1) 得到NotificationManager
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

2) 建立一個Notification
Notification notification = new Notification();
notification.icon = R.drawable.notification_icon;
or
int icon = R.drawable.notification_icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);

3) Notification的各個屬性
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

4) 發送通知
private static final int ID_NOTIFICATION = 0;
mNotificationManager.notify(ID_NOTIFICATION, notification);

Reference:
android.app.Notification @ Android SDK
【Android笔记】Notification和NotificationManager的基本使用方法


§AIDL(Android Interface Definition Language)
Android Interface Definition Language (AIDL) @ Android SDK
Android AIDL (Android Interface Definition Language)


§Generic Socket Programming tutorial
Reference: Socket Programming tutorial


§ANR(Android is Not Responding) Problem
"...將費時的工作放在 Thread.run() 中執行,還要將這個 Thread 放在 Service 中執行。因為,當螢幕要旋轉時,系統只會殺掉 Activity Stack 中,最上面的那一個 Activity,並不會對扮演執行 background task 的 Service 採取任何的動作。Service 和 Thread 還是可以安穩地繼續執行工作,而被系統重起的 Activity,也可以透過 Service 提供的 remote interface,與 Service 進行 IPC (interprocess communication),以獲取目前執行情形。..."

詳全文

留言

這個網誌中的熱門文章

逃得了一時 逃不了一世

Google Hangouts

我老闆是真男人!