2011年4月20日 星期三

android 使用標示為hide的API

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的參數

2011年4月19日 星期二

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

在專案的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;
    }

2011年4月2日 星期六

android 使用proxy連接至網路

http://forum.xda-developers.com/showthread.php?t=766569
http://darkk.net.ru/redsocks/


in nexus one(cyanogenmod 7):
  eth0/wl0.1
    wifi/softap(BCM4329)
  lo
    loopback
  rmnet0
    3G

in rooted and installed superuser phone, if run "su root", the superuser program will ask user to confirm root access via UI.

transproxy.apk
  This program use redsock internal
     This tool allows you to redirect any TCP connection to SOCKS or HTTPS proxy using your firewall, so redirection is system-wide.

  after install this program, some file will be placed in data/data/com.hasbox.tproxy/, I found there are some scripts in "files" directory
    enable transproxy
      sh data/data/com.hasbox.tproxy/files/proxy.sh start /data/data/com.hasbox.tproxy/files type=http host= port= auth=false user= pass=
      sh data/data/com.hasbox.tproxy/files/redirect.sh start http
    disable transproxy
      sh data/data/com.hasbox.tproxy/files/proxy.sh stop /data/data/com.hasbox.tproxy/files
      sh data/data/com.hasbox.tproxy/files/redirect.sh stop

  when execution /data/data/com.hasbox.tproxy/files/redirect.sh,because android bionic libc not implement getprotobyname(), so there some warning messabe
    in my environment, 3G need use proxy but wifi can direct access , so modify redirect.sh, add " -o rmnet0" in each rule, only 3G access use proxy

  bugs
    android's netd will reset ipatbles when disable tethering(ether wifi or usb), so the setting by transproxy will gone.
      must set transproxy.apk after disable tethering