顯示具有 android app 標籤的文章。 顯示所有文章
顯示具有 android app 標籤的文章。 顯示所有文章

2016年8月3日 星期三

android app 將檔案放入程式在/data下的目錄中

之前寫測試apk的經驗:

==================================
在專案的res下面增加raw資料夾
將需要的檔案放進去(不要包含".")

使用  copyFile("XXX.sh", R.raw.XXX);

存放進去的路徑可使用this.getFilesDir()取得

目錄應該是/data/data/com.YYY.ZZZ/files


    private boolean copyFile(String fName, int rawId){
     InputStream inS = null;
     OutputStream outS = null;
          byte [] buf = new byte[1024];
     int readByte = 0;
          try {
inS = getApplicationContext().getResources().openRawResource(rawId);
outS = openFileOutput(fName,MODE_PRIVATE);
while((readByte = inS.read(buf, 0, 1024)) != -1){
outS.write(buf, 0, readByte);
}
inS.close();
outS.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
          return true;
    }

android 使用標示為hide的API - java reflection

framework中,有許多function上面使用@hide標示為隱藏,

在eclipse上面使用也無法編譯

不過可以使用下列方法解決:
http://eeepage.info/java-hide-class/
http://java.sun.com/developer/technicalArticles/ALT/Reflection/


舉例來說,開關softap使用wifiManager.setWifiApEnabled(),
但setWifiApEnabled()標示hide

可以用下列方法直接呼叫:
Method mMethod = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
mMethod.invoke(mWifiManager, new Object[] { null, true });



class.getMethod()的第一個參數為method名稱(字串),後面為這個method自己所需的參數(args)
method.invoke的第一個參數為執行這個method的class, 第二個參數為物件陣列,內容是要傳進此method的參數