2014年2月18日 星期二

[程式] Android Notes

  • Intent - Forcing an App Chooser
    Intent intent = new Intent(Intent.ACTION_SEND);
    ...
    // Always use string resources for UI text.
    // This says something like "Share this photo with"
    String title = getResources().getString(R.string.chooser_title);
    // Create intent to show chooser
    Intent chooser = Intent.createChooser(intent, title);
    // Verify the intent will resolve to at least one activity
    if (sendIntent.resolveActivity(getPackageManager()) != null) {
        startActivity(sendIntent);
    }
    REF: http://developer.android.com/guide/components/intents-filters.html

  • Intent - Check if Any App Can Handle the Implicit Intent
    if (sendIntent.resolveActivity(getPackageManager()) != null) {
        startActivity(sendIntent);
    }
    REF: http://developer.android.com/guide/components/intents-filters.html

  • Intent Filter - Restricting Access to Components
    "Using an intent filter is not a secure way to prevent other apps from starting your components. Although intent filters restrict a component to respond to only certain kinds of implicit intents, another app can potentially start your app component by using an explicit intent if the developer determines your component names. If it's important that only your own app is able to start one of your components, set the exported attribute to"false" for that component."
    REF: http://developer.android.com/guide/components/intents-filters.html

  • Intent Filter - Category Default
    Android automatically applies the the CATEGORY_DEFAULT category to all implicit intents passed tostartActivity() and startActivityForResult(). So if you want your activity to receive implicit intents, it must include a category for "android.intent.category.DEFAULT" in its intent filters.
    REF: http://developer.android.com/guide/components/intents-filters.html

  • Set up an Android Project to Use the Support Library
    REF: http://developer.android.com/tools/support-library/setup.html

  • Get the value of android:background
    int clickedColor = ((ColorDrawable) view.getBackground()).getColor();
    • Use getBackground(), a method of class View, to get the instance of class Drawable
    • Use (ColorDrawable) to cast, Drawable => ColorDrawable
    • Use getColor(), a method of class ColorDrawable), to get the value of background color

  • Set Background & Text Color
    • Background - view.setBackgroundColor (int color)
      Text - textview.setTextColor(int color)
    • setBackgroundColor is a method of Type View and setTextColor is the one of Type TextView
    • input parameter could be in 2 format -
      A. Color.<color name>, where color name could be WHITE, BLACK, RED ...
      B. Color.parseColor("<color code>"), where color code is the form like #000000
    ex.
    TextView textview;
    textview.setBackgroundColor(Color.parseColor("#0000FF")); textview.setTextColor(Color.WHITE);
    (class TextView extends class View => 2nd line works)

  • Landscape & Portrait Rotation
    • Activity Lifecycle
      onCreate → onStart → onResume → (Rotate Device) → onPause → onSaveInstanceState → onStop → onDestroy → onCreate → onStart → onRestoreInstanceState → onResume
    • Keep State Information
      Method 1 - Utilize onSaveInstanceState and onRestoreInstanceState
      @Override
      protected void onSaveInstanceState(Bundle bundle){
       super.onSaveInstanceState(bundle);
       bundle.putInt("<Parameter Name>", <Parameter Value>);
      }
       
      @Override
      protected void onRestoreInstanceState(Bundle bundle) {
       super.onRestoreInstanceState(bundle);
       if(bundle.get("<Parameter Name>")!= null)
        <Parameter Value> = bundle.getInt("<Parameter Name>");
      }
      Method 2 - onConfigurationChanged()
      /*** In AndroidManifest.xml ***/
      <activity ...
                android:configChanges="orientation|screenSize" >
    • /*** In Activity (Optional) ***/
      @Override 
      public void onConfigurationChanged(Configuration newConfig) { 
       super.onConfigurationChanged(newConfig);
       ...
      }
      
    REF:
    http://blog.yam.com/ipray/article/29768416
    http://www.androidguys.com/2008/10/14/rotational-forceson-your-android-app/
    http://developer.android.com/guide/topics/resources/runtime-changes.html


  • SQLiteDatabase - query vs. rawQuery
    "query和rawQuery方法在的不同僅僅在於所需参數的不同。
    rawQuery方法係由開發者手動寫出查詢SQL,而query方法是由目標表名、where子句、order by子句、having子句等諸多子句由系統組成SQL語句。兩方法皆 return Cursor object。"
    REF: http://rritw.com/a/shujuku/DB2/20110915/128726.html

  • Intent.setClassName
    setClassName(String packageName, String className)中的packageName為AndroidManifest.xml所在的package,而非該caller檔案所在的package

沒有留言:

張貼留言