顯示具有 Android 標籤的文章。 顯示所有文章
顯示具有 Android 標籤的文章。 顯示所有文章

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年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

2011年5月26日

【Android】Layout 屬性

Android基本的Layout屬性很多,其中最常看到的是UI元素長寬之fill_parent與wrap_content,但有時還是搞不懂是UI元素的屬性,還是元素中文字的屬性。以android:layout_gravity與android:gravity來看,有加layout是指元素本身相對於它的父元素而言,它顯示的位置:

android:layout_gravity="left"

2011年4月28日

【Android】A notice about AndroidManifest.xml

Any tag in AndroidManifest.xml should be lowercase!

<service android:name = ".counterService"
            android.enabled = "true">
</service>

【Android】Android Emulater can't be started

After not normally  closing Eclipse & AVD,  I restart Eclipse & AVD then got an emulator error "emulator: ERROR: the user data image is used by another emulator. aborting".

Survey some solutions:
  1. Use "adb kill-server && adb-restart-server"                                      (Not work)
  2. Delete C:\Documents and Settings\@USER@\.android\avd  .lock files   (Work)

2011年4月26日

【Android】 Create Android API doc


Use javadoc.exe to create android API document.


Eclipse的"Project"->"Generate Javadoc"



【Android】Android Debug certificate expired

I just Run first Project then get a error: Debug Certificate expired on 2010/12/14 下午 2:13

  • Q:After installation, the android SDK wil generate a "Debug" signing certificate in a keystore called debug.keystore. The Eclipse plug-in uses this certificate to sign each application build that is generated. But, a debug certicate is onle valid for 365 days. To generate a new one you must delete the existing debug.keystore file. Its location is platform dependent - you can find it in Preferences - Android - Build - Default debug keystore.