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

2015年5月29日 星期五

Android Formatting strings declare

declare string format


<string name="msg">Hello, %1$s! You have %2$d messages.</string>
%[position]$[type]
 
Using in code:

String.format(getResource().getString(R.string.msg), name, count);

2015年5月25日 星期一

Android dp and px換算

換算公式 target px = mdpi px * ( target dpi / 160 )
為了符合原本長度
dpi高的時候要比低dpi時用更多px表示 Android API: 直接從res/dimen換成px
int px = (int) getResources().getDimension(R.dimen.XXX)
數字轉換
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, r.getDisplayMetrics())

2014年6月19日 星期四

讀取project下的檔案,getResourceAsStream() 用法

在android 開發的時候常常會需要讀取project 下的檔案,這時候就要用class loader來讀取.

有下面這兩種用法

    ClassLoader.getResourceAsStream("filepath")  

    Class.getResourceAsStream("filepath")

 

差別在於,Class是從package下當根目錄開始找檔案, class  loader則是從class path開始找(也就是說project的根目錄也包含其中)

2014年2月19日 星期三

LocalBroadcastManager 使用

LocalBroadcastManager 簡單的說就是intra-app的Broadcast 發送,用來避免Broadcast  發送到外部所造成的security issue(當然你也可以指定target或是用Permission拉...)。 下面是Android 上所提到的優點:
   
  • You know that the data you are broadcasting won't leave your app, so don't need to worry about leaking private data.
  • It is not possible for other applications to send these broadcasts to your app, so you don't need to worry about having security holes they can exploit.
  • It is more efficient than sending a global broadcast through the system. 
 簡單的說就是app內部的 Broadcast系統,不會發送到外部也不會被外部的Broadcast影響。唯一一個小小的缺點就是要自行import  Android Compatibility package, 要記得把 android-support-v4.jar 放到libs下即可。  可以看一下官方的文件LocalBroadcastManager  ,就幾個簡單的API而已。
範例
LocalBroadcastManager mLocalBroadcastManager;
public void onCreate(Bundle savedInstanceState) {
    mLocalBroadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());   //要先getInstance
}

protected void onPause() {
    mLocalBroadcastManager.unregisterReceiver(TTLNotifyReceiver);
}

protected void onResume() {  
    IntentFilter filter = new IntentFilter("com.test.hello");
    mLocalBroadcastManager.registerReceiver(receiver, filter);
}

private BroadcastReceiver receiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
 //rece the Intent
    }
};

[京都] 瑠璃光院 永觀堂 東寺

      京都的紅葉大部分都是以寺廟為中心,來記錄一下這次跑的寺廟,第一間是 瑠璃光院,這沒甚麼好說的,就是熱門景點,由於剛好是不用預約的日期,所以最好要提早去排隊,交通也不是很方便會有巴士爆滿的情況,而且門票也不便宜,不過拍出來的照片層次豐富,要不要去被騙一次就看人抉擇了。 ...