2013年1月13日 星期日

substring

 

substring (int 開頭位置, int 結束位置)

 

public String substring (int start, int end)

Added in API level 1
Returns a string containing a subsequence of characters from this string. The returned string shares this string's backing array.
Parameters
start the offset of the first character.
end the offset one past the last character.
Returns
  • a new string containing the characters from start to end - 1
Throws
IndexOutOfBoundsException if start < 0, start > end or end > length().

2012年12月6日 星期四

Synchronized, Map.containsKey()

資料來源:
http://www.jackforfun.com/2007/07/java-synchronized.html



Map.containsKey() 




圖片來源http://www.isstudy.com/java/1762.html

 

http://blog.sina.com.cn/s/blog_6168ee920100jqu4.html

我们常常使用 Map 对象来缓存数据, 比较常见的处理流程是:
1. 调用 Map 的 get() 方法获取数据;
2. 如果返回不为 null, 直接返回该数据;
3. 如果返回为 null, 则生成数据, 或者从其他地方获取数据, 然后存放入 Map 中, 最后返回该数据.
这里, 我们可以通过使用 Map 的containsKey() 方法来检测是否数据是否存在, 如果key存在, 则表明已经获取过一次数据, 那么直接返回该 key 在 Map 中的值. 不管是否为 null 都直接返回; 如果 key 不存在, 则去生成或者获取数据, 并放入到 Map 中, 并返回该数据.
这里使用 containsKey() 来检测可以应用于: 1. 从其他对方获取的数据可能为空, 并且不会有变化; 2. 获取数据比较耗时. 这个场景下, 使用该方法可以大大降低消耗, 特别是在同步情况下.

MessageDigest

java.security
Class MessageDigest

java.lang.Object
  extended byjava.security.MessageDigestSpi
      extended byjava.security.MessageDigest 
 
 This MessageDigest class provides applications the functionality of a
 message digest algorithm, such as MD5 or SHA. 
 
 MessageDigest md = MessageDigest.getInstance("MD5");
 byte[] hashConde;
 md.update(number.getBytes());
 hashConde = md.digest(); 
 


 byte[] digest()
          Completes the hash computation by performing final operations such as padding. 取的值須儲存為byte[] 型態
 
 
參考 http://caterpillar.onlyfun.net/Gossip/Encoding/String.html
 byte[] getBytes()
          Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array. 使用平台預設的編碼, 儲存為byte陣列
 




參考 http://www.dotblogs.com.tw/chhuang/archive/2011/01/19/20883.aspx
 void update(byte[] input)
          Updates the digest using the specified array of bytes. 計算byte[] 的演算法的值(MessageDigest)

2012年9月17日 星期一

在webmin設定DNS

http://antoinecafe.blogspot.tw/2004/09/webmindns.html

啟用 chroot 功能後, 所有與該程式運作相關的檔案必須全部移至更改後的根目錄下。以此版 BIND 為例, 所有設定檔及記錄檔都必須以 /var/named/chroot 為根目錄, 搬到該目錄下 (檔案的內容則不需更改)。相關檔案要擺位的位置, 請參考下表: 相關檔案名稱 啟用 chroot 前檔案 (絕對) 位置 啟用 chroot 後檔案 (絕對) 位置 
工作環境設定檔 
/etc/named.conf 
/var/named/chroot/etc/named.conf 

根網域記錄檔 
/var/named/named.ca 
/var/named/chroot/var/named/named.ca 

本機正解記錄檔 
/var/named/localhost.zone 
/var/named/chroot/var/named/localhost.zone 

本機反解記錄檔 
/var/named/named.local 
/var/named/chroot/var/named/named.local 

網域正解記錄檔 
/var/named/named.flag.com.tw 
/var/named/chroot/var/named/named.flag.com.tw 

網域反解記錄檔 
/var/named/named.203.74.205 
/var/named/chroot/var/named/named.203.74.205 

2012年9月10日 星期一

intent中的俩方法区别


http://zhouxun1026.iteye.com/blog/1618618

public Intent putExtra (String name, double[] value)
设置方法 intent.putExtra("aaa", "bbbb");
获取方法 this.getIntent().getCharSequenceExtra("aaa")

public Intent putExtras (Bundle extras)

设置方法
Bundle bd = new Bundle();
bd.putString("aaa",“bbbb”);
intent.putExtras(bd);
获取方法
Bundle bd=this.getIntent().getExtras();
bd.getString("aaa"));


总结:带s的得通过个Bundle来绑定数据

2012年8月27日 星期一


Lifecycle Flow

Here are the primary processes involved in cloud-to-device messaging:
  • Enabling GCM. An Android application running on a mobile device registers to receive messages.
  • Sending a message. A 3rd-party application server sends messages to the device.
  • Receiving a message. An Android application receives a message from a GCM server.
These processes are described in more detail below.

Enabling GCM

This is the sequence of events that occurs when an Android application running on a mobile device registers to receive messages:

  1. The first time the Android application needs to use the messaging service, it fires off a registration Intent to a GCM server.
    This registration Intent (com.google.android.c2dm.intent.REGISTER) includes the sender ID(註冊後網址列上的ProjectID), and the Android application ID.(APP名稱)
    Note: Because there is no lifecycle method that is called when the application is run for the first time, the registration intent should be sent on onCreate(), but only if the application is not registered yet.
  2. If the registration is successful, the GCM server broadcasts a com.google.android.c2dm.intent.REGISTRATION intent which gives the Android application a registration ID.
    The Android application should store this ID for later use (for instance, to check on onCreate() if it is already registered). Note that Google may periodically(定期) refresh the registration ID, so you should design your Android application with the understanding that the com.google.android.c2dm.intent.REGISTRATION intent may be called multiple times. Your Android application needs to be able to respond accordingly.
  3. To complete the registration, the Android application sends the registration ID to the application server. The application server typically通常 stores the registration ID in a database.
The registration ID lasts持續 until the Android application explicitly明確地
 unregisters itself, or until Google refreshes the registration ID for your Android application.
Note: When users uninstall an application, it is not automatically unregistered on GCM. It is only unregistered when the GCM server tries to send a message to the device and the device answers that the application is uninstalled. At that point, you server should mark the device as unregistered (the server will receive a NotRegistered error).
Note that it might take a few minutes for the registration ID to be completed removed from the GCM server. So if the 3rd party server sends a message during this time, it will get a valid message ID, even though the message will not be delivered to the device.