搜尋此網誌

2014年10月10日 星期五

【Android】PendingIntent介紹和用法


  • Intent 是及時啟動的,intent隨activity消失而消失;PendingIntent可以看作是對intent的包裝。
  •  Intent 一般是用來作Activity、 Service、BroadcastReceiver之間傳遞數據,而Pendingintent,一般用在Notification上,可以理解為延遲執行的intent。


  • 簡單來說,Intent指定好要幹嘛後,就去做了。而PedingIntent則是先把某個Intent包好,丟給某個程式,以後再去執行Intent要幹嘛。比如用startActivity(intent)就會直接去啟動和intent關聯的某個程式了。相對的,假設APK1想告訴APK2在某個情況 下,去執行APK3的話,就可以在APK1設定好PendingIntent(內容就是去執行APK3),然後丟過去給APK2,然後APK2在某種情況 下,就去執行PendingIntent,那就會等於執行APK3。這裡不單單是指startActivity,也可以是對Broadcast和 Service進行處理。

  • 那為何不直接在APK2就用startActivity(Intent)寫好要執行 APK3,不就不用丟PendingIntent了?因為APK2要「動態」決定開啟,不同情況下要執行那一支APK,所以不能先寫死。假設使用 startActivity的情況下,如果有A1、B1、C1三支程式都要APK2去執行不同程式,分別為A3,B3和C3,那不就變成APK2的程式要 寫上startActivity(A3)、startActivity(B3)、startActivity(C3)。假設未來還會有新的D3、E3…會 被執行的話,APK2的程式碼,不就無法要不停的調整了。所以利用PendingIntent就可以解決這個問題。
以上節錄自:
http://developer.android.com/reference/android/app/PendingIntent.html
http://sunzone.iteye.com/blog/1998091
http://androidgill.blogspot.tw/2011/12/pendingintent1.html

2014年10月9日 星期四

【Android】Fragments簡介

適用於Android 3.0 (API level 11) and higher


http://jacky-google-android.blogspot.tw/2011/04/android-fragments.html
http://soldierzx0705.blogspot.tw/2013/07/preferencefragment.html

【Android】Class_Name.this的使用

在閱讀Android API Guide

時常看到Class_Name.this的使用

這個用法多用於在nested class中

當inner class必須使用到outer class的this instance時

舉例來說


class Outer{
    String data = "外部類別";

    public class Inner{
        String data = "內部類別";
        public String getOuterData(){
            return Outer.this.data;
        }
    }
}

2014年9月26日 星期五

【Note】重灌電腦




瀏覽器

一般/常用
  • Malwarebytes Anti-Malware
  • Comodo
  • Avira
  • Revo Uninstaller
影音剪輯
  • AvidemuxPortable_32bit_2.6.8
  • FreemakeVideoConverter_4.1.3.14
  • Yamb-2.1.0.0_beta2
影音播放
  • KMplayer 3.0.0.1438
  • PotPlayer



2014年9月23日 星期二

【Android】onClick新舊方法比較

節錄自:
http://developer.android.com/reference/android/widget/Button.html
http://blog.chinatimes.com/tomsun/archive/2010/11/09/560824.html

Represents a push-button widget. Push-buttons can be pressed, or clicked, by the user to perform an action.
A typical use of a push-button in an activity would be the following:
 public class MyActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);

         setContentView(R.layout.content_layout_id);

         final Button button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
             }
         });
     }
 }
However, instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick attribute. For example:
 <Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="@string/self_destruct"
     android:onClick="selfDestruct" />
Now, when a user clicks the button, the Android system calls the activity's selfDestruct(View) method. In order for this to work, the method must be public and accept a View as its only parameter. For example:
 public void selfDestruct(View view) {
     // Kabloey
 }
The View passed into the method is a reference to the widget that was clicked.
舊的方法:
以按鈕介面元件所提供的”setOnClickListener(View.OnClickListener)”方法,為該按鈕設置一個”OnClick監聽器”(OnClickListener)。
程式開發者才可以在該方法之中編寫”onClick(View)”方法裡頭的運算程式碼。
不過,這種方式除了會為這個”OnClick監聽器”多幾行程式碼之外,主要還會有一個缺點,那就是無論按鈕有沒有被按下,在應用程式被執行時,Android系統都需要為按鈕產生一個”OnClick監聽器”

假若,介面中有十個按鈕,那等於在”onCreate(Bundle)”覆寫方法被執行時,就要產生十個”OnClick監聽器”。

新的方法:
當手機用戶按下按鈕時,便會直接呼叫該按鈕的方法,並執行method 裡頭的程式運算動作。如此一來,就可免除了”OnClick”監聽器的使用。

因為,該對應之方法是獨立撰寫於”onCreate(Bundle)”覆寫方法之外,而非寫在”onCreate(Bundle)”覆寫方法裡頭。所以,當”onCreate(Bundle)”覆寫方法被載入時,Android系統並不會將它馬上載入Activity生命週期的運作當中。

不過,此法有個缺點,那就是Android 1.1(API Level 2)與Android 1.5(API Level 3)並不支援
宣告的method務必使用public

2014年9月21日 星期日

【Android】onClickListener的事件處理

參考網址:
https://www.ptt.cc/bbs/AndroidDev/M.1326163107.A.BDA.html

【Android】FILL_PARENT 和 WRAP_CONTENT 的差別



  • FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding)
  • WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content (plus padding)
  • 用一張圖來說明:





    2014年9月18日 星期四

    【Android】解決"Your project contains errors,please fix them before running your application."

    Window -> Show View -> Problems 點選後,看是哪發生Errors
    結果:Error generating final archive: Debug Certificate expired on 2012/5/23 
    此句話意思是無法產生.apk檔:Debug認證已經過期  距離上次摸android至今也有兩年了

    所以解決辦法需要讓android sdk 重新生成debug.keystore
    以下參考網址:
    http://blog.sina.com.cn/s/blog_6d0021460100ysbd.html
    http://q-and-g.blogspot.tw/2009/07/androidkeystore.html

    - preference -> android -> build中可以找到舊的debug.keystore位置
    - Windows -> Preferences -> Java -> Installed JREs 確認Jre Y 資料夾的location位置,找到bin -> keytools 
    - 進入console mode ,在上述路徑下輸入
    keytool -genkey -v keystore debug.keystore -alias developer.keystore -keyalg RSA -validity 20000
    其中debug.keystore可自訂名稱。

    keystore:名稱
    alias:別名
    keyalg:演算法
    validity:有效天數

    輸入後,會有一系列的問題,需要注意的是過程中會輸入兩種密碼,網路上書上都推薦怕記不住就用一樣。

    【Android】Eclipse中,找不到android SDK and AVD Manager選項

    在Window -> Customize Perspective -> Command Groups availability-> 把 Android SDK and AVD Manager 打勾 -> OK