2012年8月23日

【Android】Android 3.0 使用WebService 發生android.os.NetworkOnMainThreadException異常

WebService (包含HTTP Request等)在2.x以前可以正常運作,但在Android 3.0以後會出現
下列這問題: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
原因在於從API 9開始,android 對 Thread Policy有所改變,導致對Network存取會發生不允許的現像。

Solution:
 在onCreate()加入下段程式


    if (android.os.Build.VERSION.SDK_INT > 9) {
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
      StrictMode.setThreadPolicy(policy);
    }
Reference:


2012年8月2日

【Android】Create a new Activity



You can use the "New Class" dialog, but that leaves other steps you need to do by hand (e.g. adding an entry to the manifest file). If you want those steps to be automated, you can create the activity via the manifest editor like this:
  1. Double click on AndroidManifest.xml  in the package explorer.

2012年6月18日

【Andoid】eclipse connect to mobile device

1.Turn on "USB Debugging" on your device 
     Android 2.X :Settings > Applications > Development enable USB debugging 
     Android 4.0 :Settings > Developer enable USB debugging 

2.Set up your system to detect your device
     Windows:install USB Driver
     Mac OS X:It works, skip it.

Reference:

【Android】R.java is missing?

When creating a new Android project, but got an error, can't find 'R'. Tracing the project, we can find that R.java is missing.

This is due to setup incorrect target build settings. Target build was set toAndroid 2.1 (SDK v7) where his layout XML used Android 2.2 (SDK v8) elements (layout parametermatch_parent), due to this there was no way for Eclipse to correctly generate the R.java file which caused all the problems.

Solution:

  • Remove  import android.R.*
  • Project->Clean , to regenerate R.java
Reference:

2012年4月18日

【HTML】How to know IE and add code

IE、Chrome 和 Firefox對javascript/css的支援度都不一樣,有時css在IE是正常,但在Firefox下卻亂掉,常常讓人非常困擾,一般而言,IE對JavaScript跟CSS支援度最差,如果能判斷Browse是否為IE再載入特定的CSS或JavaScript便能解決問題。

目前最常見用來判斷Browser類型的有兩種語法(JavaScript/HTML),建議用HTML語法來寫,因為不管是那種Browser都通用:

1。檢查是否為IE



2。檢查IE的版本




2012年4月3日

【Linux】Ubuntu 10.04 LTS install sun-java-sdk


-----2012.04.03 Update-------------------------------------------------------------
由於最近canonical remove all Sun JDK packages from the Partner archive.
所以只能透過PPA來install sun java 6

 sudo apt-add-repository ppa:flexiondotorg/java 

 sudo apt-get update 

 sudo apt-get install sun-java6-jdk sun-java6-plugin  

 sudo update-java-alternatives -s java-6-sun

Reference:


2012年2月10日

【Android】Signing Your Applications

在寫好Android app時,若要將它上架至Market上,要先使用正確的Keystore將
APK sign好才能上架,通常在開發app跑在avd所用的都是debug.keystore,不能用
於正式上架的APK。

1。generate Keystore


  • Use JDK tool - keytool  to generate private key
  • keytool -genkey -v -keystore key-file.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 365
  • keytool要求輸入密碼與其它資訊,365為有效期限,可以設大一點,如:Insyde market就需要20年的有效期限。


2。use eclipse to sign android app
     在Package Explorer上點選Project
      File-->Export-->Android/Export Android Application
      再依照wizard進行輸入Private key 密碼。


Reference:
Android Dev: Signing Application
Android Dev: Signing Your Application            

2012年2月8日

【Android】Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE


When running android application on AVD, the error shows that

[2012-02-08 22:18:15 - ShowPhotos] Installing ShowPhotos.apk...
[2012-02-08 22:18:17 - ShowPhotos] Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE
[2012-02-08 22:18:17 - ShowPhotos] Please check logcat output for more details.
[2012-02-08 22:18:17 - ShowPhotos] Launch canceled!

Solution:ON AVD->Settings->Applications->Manage applications->remove the app->Run again