Social Icons

2013年7月14日 星期日

今天我學到 - ADT升級到22,libraries在編譯時不會被加入apk的問題。

最近將ADT升級到v22時,發現程式編譯起來怪怪的,一下是library找不到,一下子執行的時候又缺乏核心元件,搞的我心浮氣躁,差一點把電腦給砸了。

後來google了一下,發現已經有很多開發者在討論這個問題了,原文在此
簡單的說,當升級ADT 22之後,在Java Build Path裡頭,原本的應該link進去的library沒有被加進去。檢查和加入的步驟如下:

1.  將滑鼠移到Project上,點擊滑鼠右件,選擇『Properties』,叫出Properties視窗。
2. 點擊『Java Build Path』設定
3. 點擊『Order and Export』標籤
4. 確認『Android Private Library』有被選取
5. 點擊『OK』鍵,完成設定
6. 將滑鼠移到Project上,點擊滑鼠右件,點擊『refresh』,然後清除Project,並重新編譯。

我這樣試過之後就可以了,您也可以試一試。

2013年5月29日 星期三

今天我學到 - 在RelativeLayout中,如果使用include的時候,如何對元件佈局

今天在該程式界面的佈局,其中發生一個問題,我在RelativeLayout中用了2個include,原本想要讓他們上下排排站,卻發現跌在一起。原本的佈局如下:
  <RelativeLayout 
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="4dp"
        android:orientation="vertical"
        >
        <include android:id="@+id/distance_panel"
            android:layout_alignParentTop="true"
         android:layout_alignParentLeft="true"
         android:layout_alignParentRight="true"
            layout="@layout/float_panel_name_low" />
        <include android:id="@+id/time_panel" 
            android:layout_below="@id/distance_panel"
         android:layout_alignParentLeft="true"
         android:layout_alignParentRight="true"
            layout="@layout/float_panel_name_low" />

後來查了一下,發現要把layout_width和layout_height也補上才會對,又學了一招。
   <RelativeLayout 
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="4dp"
        android:orientation="vertical"
        >
        <include android:id="@+id/distance_panel"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
         android:layout_alignParentLeft="true"
         android:layout_alignParentRight="true"
            layout="@layout/float_panel_name_low" />
        <include android:id="@+id/time_panel" 
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
            android:layout_below="@id/distance_panel"
         android:layout_alignParentLeft="true"
         android:layout_alignParentRight="true"
            layout="@layout/float_panel_name_low" />

2013年5月22日 星期三

今天我學到 - 不同Android版本的Fragment裡的onCreateOptionsMenu和onResume呼叫順序會不一樣


今天在查一個程式崩潰的問題,查到後來是因為當系統呼叫onResume時,一個menu item還未被建立,然後在onResume裡直接對這個null物件操作,所以造成程式崩潰。不過我在我的三台手機和模擬器都沒有問題啊,奇怪!

解決的方法就是多加個判斷就可以了。
  super.onResume();
  if (_new_record_menu!=null)
   _new_record_menu.setVisible(_show_new_record_menu);

今天我學到 - Service onStart程序內不要做loading太重的事

在我的計劃裡面,主程式起來前會呼叫一個service來完成一些上傳的動作,但是常常會讓我的主程式跑出UI freeze的對話框。用Google查了一下,Service的程序是會影響主程序的,如果要操作比較耗時的工作,還是要再另外起一個Thread會比較好啊。

原本的程式:
 @Override
 public void 
 onStart(Intent intent, int startId) {
  super.onCreate();
  showNotification();
  
  trainingId = -1;
  while (isConnect()==true &&          // still connect with internet 
    _error_occurred == false &&        // no error occurred.
    (trainingId=providerUtils.getLastTrainingId())>0) {  // can get any training record.
   
   if (_bg_h!=null) {
    _is_upload = true;
    _start_upload_time = System.currentTimeMillis();
    _bg_h.post(do_uploadTraingRecord);
    while (_is_upload==true && 
     _error_occurred==false) {
     if ((System.currentTimeMillis()-_start_upload_time) > max_waiting_time) {
      _is_upload = false;
      _error_occurred = true;
     }
     else {
      try {
       Thread.sleep(1000);
      } 
      catch (InterruptedException e) {
       e.printStackTrace();
       _error_occurred = true;
      }      
     }
    }
   }
   else {
    // no background handler.
    break;
   }
  }
  stopService();
 }
會發現有一個while迴圈,這就是造成UI凍結的兇手,只要把它搬到thread去就好了。

更改過後:
 @Override
 public void 
 onStart(Intent intent, int startId) {
  super.onCreate();
  showNotification();
  
  if (isConnect()==false || providerUtils.getLastTrainingId() <=0 || _bg_h==null) {
   stopService();
  }
  else {
   _bg_h.post(run_uploadAll);
  }
 }

2013年5月15日 星期三

今天我學到 - 如何讓Google map view (v2),在滾動頁面時不會產生殘影

使用Google map api v2時,發生了一個大問題,程式中的map view不是佔據整個頁面,只是其中的一部分。這樣問題就來了,當滾動(Scrolling)時原本map view的位置就會產生殘影,整個畫面就被破壞掉了。(後來發現如果將Google Map View放在ViewPager,也會有類似的問題)  

2013年5月7日 星期二

今天我學到 - Hardware acceleration和WebView

程式裡因為改用Google Map api V2時,就忘了將Hardware acceleration設成off,就在測試時發現WebView會不定時的crash,google了一下找到這篇,說WebView使用硬體加速會有很多問題,建議取消。但是,因為程式中的Google Map api v2用到openGL,所以如果Hardware acceleration設成on的話是會讓整個速度提升不少的。所以就花了一點時間查詢如何在外部和程式中設定是否使用硬體加速的功能。

在AndroidManifest.xml中設定

a.整個application設定
<application android:hardwareAccelerated="true" ...>
b.對activity
<activity android:hardwareAccelerated="false" />
您可以在application中設定整體是否要加速,然後在activity中對分別activity來設定。但是,如果您想要在程式中設定,或者只針對某一個View來設定呢的話,可以參考下面程式碼:
// 在window中只能設定硬體加速,不能取消哦。
getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);

// 在view中可以取消硬體加速
myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

2013年5月6日 星期一

2013年5月2日 星期四

今天我學到 - Google Map API v2如何Zoom到指定位置和指定區塊

將程式裡的Google Map從原本的v1,移植到v2的版本上面來,發現還不是非常容易哩。原本v1裡面的一些程式碼都不能用了,全部都的改成v2的思維才行。


2013年5月1日 星期三

今天我學到 - 申請Android Google Map API v2

一直以來都使用初始的Google map api,不過這個版本在遇到新的Framework就有些適應不良了。也就是說如果在有Map的時候,要使用MapActivity,就不能和一般的Activity放在一起了,會增加程式的複雜度。另外,如果將Google Map用在Fragment中就必須使用MapFragment才行。

一旦,要將程式相容於Android 2.x時,一切都會變得很複雜了。。。

新的計劃裡要用HoloEverywhere + Google map api V2來完成,所以需要申請新的key,查了一下,摩刻部落已經將申請api key的程序寫的很完整了,放上連結,以便往後查詢。

2013年4月30日 星期二

今天我學到 - 在模擬器裡面測試Google map api V2

Google推出map api V2一段時間了,最近想要把程式中的Google map改成v2的,但是發現新的Api在模擬器是無法顯示出來的,會出現下面訊息:


2013年4月29日 星期一

今天我學到 - PagerAdapter (ViewPager) throwing IndexOutOfBounds exception

今天在重新組織一段程式碼的時候,程式一直發生當機的情形,看了一下 called stack,發現程式中使用了ViewPager,其中PagerAdapter的instantiateItem函數裡的((ViewPager)collection).addView會讓程式當掉,丟出IndexOutOfBounds的例外,查了一下發現不能像下面這樣使用:

  public Object 
  instantiateItem(View collection, int position) 
  {
                        ...
   ((ViewPager)collection).addView(view, position);
   return view;
  }

2013年4月26日 星期五

今天我學到 - 在Eclipse設定NDK編譯的環境

負責的計劃中,需要使用到c/c++的程式碼,但是在Eclipse中如果要讓Android project在編譯時可以同時將c/c++的程式碼一起編譯需要做一些設定才行。
下面就是學到的設定方法:

步驟一:將Android計劃Convert成C/C++的計劃。
您可能會擔心轉換成C/C++計劃會不會原本Android計劃就被改變了。。。。答案是不會!:p
 
(File -> New -> Other...)

2013年2月17日 星期日

讓剪貼簿Clipboard相容於2.x

在寫程式的時候,會需要將文字資料複製到剪貼簿的時候,不同Android版本會產生問題。
原因就是Honeycomb(v3.0)之後,Android使用了新的剪貼簿,所以在程式裡要判斷新舊方法的使用時機,程式片段如下:
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
     if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB){
          android.content.ClipboardManager clipboard =  (android.content.ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
             ClipData clip = ClipData.newPlainText("Marathon's world", subject);
             clipboard.setPrimaryClip(clip); 
     } 
     else{
         android.text.ClipboardManager clipboard = (android.text.ClipboardManager)getSystemService(CLIPBOARD_SERVICE); 
         clipboard.setText(subject);
     }

2013年2月16日 星期六

啟動螢幕Splash Screen的設計

啟動螢幕Splash Screen常常用在程式一開始的時候,特別是用來顯示公司的logo。 這邊要介紹實作splash screen的兩種方式,第一種是利用兩個不同的xml layout檔案,第二種是寫在同一個xml layout檔案中。



如何判斷float的NaN

在我的程式中為了要顯示先現在為止位置的方向,需要得到compass或GPS的bearing,然後在利用matrix中rotate的功能將方向的圖示圖示做對應的選zhau旋轉。可是有時候程式會無法得到正確bearing的值,如果將NaN的值塞到rotate中會造成程式crash。Google了一下原來在Java中要判斷float的NaN,還是要用到一些技巧的。 

也還蠻簡單的,不過要利用Float object來判斷,程式碼如下:

Float dbear = new Float(this.myloc.bearingTo(oxlocate));
if (dbear.isNaN() == false) {

GeoCoder,判斷是否可以連上Internet兩三事

為什麼會將GeoCoder和Internet一起將起講,是因為使用GeoCoder造成我的程式會hang住,查了一陣子才發現和網路有關所以就在這裡一起談談了。 話說,Geocoder是Google提供的一個可以輸入坐標位置,然後fanc反查出地址的強大服務,不過小弟用了之後卻發生水土不服,而造成程式上吐下瀉的情況,原因是網路部位不穩,或者無法存取網路的時候,會讓程式停在哪裡,然後系統就會跳出程式無回應的警告視窗。 為什麼會這樣呢,其實是程式中會註冊座標移動的callback,當座標位置一旦有改變時就會呼叫這個callback。好死不死在這個callback中會呼叫GeoCoder來反查地址,並改變UI的文字。就這樣一旦網路不通的時候,就讓程式停在那裡了。 所以說,如果要使用GeoCoder一定要善用,只好另起一個Thread,背景執行,或者檢察現在網路是否有通嘍,下面就是檢察網路是否有通的程式碼了。
public boolean isInternetConnect() {
  boolean isConnected = false;
  ConnectivityManager connec =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connec.getNetworkInfo(0).isConnectedOrConnecting() == true ||  
         connec.getNetworkInfo(1).isConnectedOrConnecting() == true  ) {
         isConnected = true;
        }
        return isConnected;
}
其中connec.getNetworkInfo(0)是指Mobile phone(GPRS,3G),connec.getNetworkInfo(1)是檢察WiFi有沒有通。

Google Map Key - 您輸入的指紋無效

申請Google Map Key卻出現,對不起您輸入的指紋無效? 最近重新申請Google Map Key,照往例用keytool列出debug.keystore的憑證指紋,卻沒想到在申請頁面一直出現『您輸入的指紋無效;請按瀏覽器的 [上一頁] 按鈕,並輸入有效的憑證指紋。』的訊息。沒錯啊,整個過程都沒錯啊,怎麼會這樣? 後來看到檢察了一下發現奇怪我的key怎麼特別的長?才發現原來申請Google Map Key的憑證指紋要是MD5的,

而keytool列出來的是SHA1,
 

怎麼會這樣呢?以前都沒有問題啊!原來最近將JDK升級到版本7。結果呢!在版本7的keytool預設的輸出是SHA1,我的嗎啊!怎麼會這樣呢?需不需要降級到版本6啊?還好查了一下,只要在執行時加上 -v 這個參數就可以將所有編碼的憑證指紋都列出來了。

下回還是不要亂升級了,會搞死人的。 :)

兩個View切換中,如何產生平滑效果

因為目前在開發的程式中,有許多視窗,而使用者需要在這些視窗中切換,為了讓程式跑的順一點,並沒有create很多的activity,而是透過且切換View來達成。 但是在View切換過程,如果是使用動畫來表現的話,應該是比較好看的,所以趁這個機會研究一下Android的動畫,這邊要講的是如何在不同View之間,產生平滑移動的小效果。 首先,我們先建立幾個動畫XML檔(請放在/layout目錄下) left_in.xml
<?xml version="1.0" encoding="utf-8"?>   
<set 
        xmlns:android="http://schemas.android.com/apk/res/android">   
    <translate 
            android:fromXDelta="-100%p" 
            android:toXDelta="0"  
        android:duration="500" />   
  
</set>
left_out.xml
<?xml version="1.0" encoding="utf-8"?>   
<set 
    xmlns:android="http://schemas.android.com/apk/res/android">   
    <translate 
            android:fromXDelta="0" 
            android:toXDelta="-100%p"  
        android:duration="500" />
</set>
right_in.xml
<?xml version="1.0" encoding="utf-8"?>   
<set 
        xmlns:android="http://schemas.android.com/apk/res/android">   
    <translate 
            android:fromXDelta="100%p" 
            android:toXDelta="0"  
        android:duration="500" />   
  
</set>
right_out.xml
<?xml version="1.0" encoding="utf-8"?>   
<set 
    xmlns:android="http://schemas.android.com/apk/res/android">   
    <translate 
            android:fromXDelta="0" 
            android:toXDelta="100%p"  
        android:duration="500" />
</set>
在main.xml我們定義了兩個Views,一個一開始是顯示的,另一個是隱藏起來的,另外定義了兩個按鈕來做視窗的切換。
.....
     <LinearLayout
      android:id="@+id/map_view"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
   android:layout_marginTop="0dip"
   android:layout_marginBottom="0dip"
   android:layout_marginLeft="3dip"
   android:layout_marginRight="3dip"
   android:layout_weight="1"
   android:visibility="gone"
      >
     </LinearLayout>
     <ListView android:id="@+id/oxcache_list"
   android:persistentDrawingCache="animation|scrolling"
   android:cacheColorHint="#00000000"
   android:divider="@color/gray_light"
   android:dividerHeight="1dip"
   android:orientation="vertical"
   android:layout_weight="1"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">
  </ListView>
.....
     <Button   
         android:id="@+id/list_btn"     
         android:layout_width="wrap_content"    
         android:layout_height="wrap_content"    
         android:layout_marginRight="0dip"
         android:text="@string/list"
         android:textColor="@color/header_text"
         android:background="@drawable/btn_left_unpress"
         style="@style/List"
     />   
     <Button   
         android:id="@+id/map_btn"     
         android:layout_width="wrap_content"    
         android:layout_height="wrap_content"
         android:layout_marginLeft="-8dip"
         android:text="@string/map"
         android:textColor="@color/header_text"
         android:background="@drawable/btn_right_unpress"
         style="@style/List"
     />
在程式中,透過監聽兩個按鈕的click event來切換不同的視窗,重點是什麼情況是右進左出,左進右出了。
listBtn.setOnClickListener(new Button.OnClickListener() {
   @Override
   public void onClick(View v) {
           mOxcacheList.setVisibility(View.VISIBLE);
           mOxcacheList.setClickable(true);
           mOxcacheList.setAnimation(AnimationUtils.loadAnimation(CacheMe.this, R.layout.push_left_in));
           mMapLinear.setVisibility(View.GONE);
           mMapLinear.setAnimation(AnimationUtils.loadAnimation(CacheMe.this, R.layout.push_right_out));
    }
   }
        });
  mapBtn.setOnClickListener(new Button.OnClickListener() {
   @Override
   public void onClick(View v) {
           mMapLinear.setVisibility(View.VISIBLE);
           mMapLinear.setClickable(true);
           mMapLinear.setAnimation(AnimationUtils.loadAnimation(CacheMe.this, R.layout.push_right_in));
           mOxcacheList.setVisibility(View.GONE);
           mOxcacheList.setAnimation(AnimationUtils.loadAnimation(CacheMe.this, R.layout.push_left_out));
    }
   }
        });