2011年12月8日 星期四

AIDL, couldn't find import for class sni.lib.Note.GestureOperation

這個問題與路徑有關, 在函式庫的配合時,
sni.lib.Note..........class GestureOperation
com.sni.Note.......package to host the Service
sni.lib.Page..........package as Client to link the Service

製作了一個AIDL在com.sni.Note,
package com.sni.Note;
import com.sni.Note.GestureOperation;
interface INoteService {
 GestureOperation[] getOperations();
 }
出現
couldn't find import for class sni.lib.Note.GestureOperation
的錯誤,

然後, 加上GestureOperation.aidl的AIDL檔:
parcelable GestureOperation;
原來的錯誤沒有了,
但是所產生(gen)的java檔報錯,
GestureOperation cannot be resolved to a type
並要求import,
如果用了import, 那就違反了檔頭所說的:
* This file is auto-generated.  DO NOT MODIFY.
這是因為 GestureOperation.aidl 和GestureOperation.java不在一起,
怎麼辦?

將GestureOperation.aidl移至sni.lib.Note, 讓他們生活在一起,
當然, import的路徑要改,
import com.sni.Note.GestureOperation
結果, 原來import的錯誤又回來了,
如果在GestureOperation前加上package, 如:
package sni.lib.Note;
parcelable GestureOperation;
結果出現新的錯誤:
interface GestureOperation should be declared in a file called sni\lib\Note\GestureOperation.aidl.

所以, 暫時看起來沒有解, 有網路文章說, 與路徑有關, 就是GestureOperation放到sni.lib.Note, 但是 aidl compiler找不到, 目前有一個解, 是可以用, 但是如果有多個class要同時使用時, 就要費一點腦子,
sni.lib.Note .............. 放 GestureOperation (java & aidl), INoteGesture.aidl
com.sni.Note ........... 定義Service, 這是因為所需的資料在這個類別裡,
sni.lib.Page .............. 執行 bindService
這樣子, 是可以正常執行的, 因為
INoteGesture ..... GestureOpeartion.aidl ....... GestureOperation.java
都在一起, 避開了路徑的問題

另外在Serivce, action的定義名稱是自取的, 可以與package名稱不相干.

在bindService時也出現錯誤, (回傳 false), 這是因為在GroupActivity底下的Activity是無法使用bindService, 在我的個案中, 使用
getApplicationContext().bindService 依然不行, 但是使用
getParent().bindService就可以了, 這個在網路上有比較完整的討論,
http://blog.tourizo.com/2009/04/binding-services-while-in-activitygroup.html
http://randomizedsort.blogspot.com/2010/11/service-binding-workaround-in-tab.html

還有一點必須注意的是, bindService傳回true, 不代表就可以使用service interface, 必須等onConnection完成. 這是容易理解.



2011年12月6日 星期二

getLaunchIntentForPackage return null

1.確定被呼叫的APP有安裝 (getInstalledPackage)
2.確定被呼叫的APP可以正常執行 (MAIN, LAUNCHER有設定)
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
可是....就是回傳null,

結果發現是:
    <data android:scheme="id" />
在作怪, 只要有這一行, 就回傳null, 換句話說, 若是有 data/scheme就不能用getLauncherForPackage,

所以, 就改用 setClass
     Intent intent = new Intent(Intent.ACTION_MAIN);
     intent.setClassName(packageName, activityName);
     intent.setData(Uri.parse("id://"+id));
     startActivity(intent);
搞定

attempt to write a readonly database

錯誤:
12-06 10:16:43.523: E/AndroidRuntime(286): java.lang.RuntimeException: Unable to start activity ComponentInfo{sni.Note.Comm/sni.Note.Comm.CommActivity}: android.database.sqlite.SQLiteException: attempt to write a readonly database: BEGIN EXCLUSIVE;

1.確定是以ReadWrite的方式開啟的,
2.不應該要設定檔案夾或是檔案的權限, 因為這是在手機, 使用者是可能不會設定, 而且, 以前也不用,

搞了很久, 真是要去撞牆, 原來是權限忘了設:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>