隨筆雜記

             :: 首頁 :: 新隨筆 ::  ::  :: 管理 ::

          #

          # 獲取屏幕亮度

          public static int getScreenBrightness(Activity activity) {
          int value = 0;
          ContentResolver cr = activity.getContentResolver();
          try {
          value = Settings.System.getInt(cr, Settings.System.SCREEN_BRIGHTNESS);
          } catch (SettingNotFoundException e) {

          }
          return value;
          }

          # 設置屏幕亮度

          public static void setScreenBrightness(Activity activity, int value) {
          WindowManager.LayoutParams params = activity.getWindow().getAttributes();
          params.screenBrightness = value / 255f;
          activity.getWindow().setAttributes(params);
          }

          posted @ 2012-03-13 14:49 天宇恒星 閱讀(5425) | 評論 (1)編輯 收藏

          默認的Android的Browser中無法設置User Agent,但是在Browser的源碼中可以看到,android提供了4中User Agent 具體代碼如下:

          在Browser的 src/com/android/browser/BrowserSettings.java 

          private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +  
                     
          "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +  
                     
          "like Gecko) Version/5.0 Safari/533.16";  
            
             
          private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +  
                     
          "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +  
                     
          "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";  
            
             
          private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +  
                     
          "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +  
                     
          "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";  
            
             
          private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +  
                     
          "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +  
                     
          "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";  



          提供了4中不同的User Agent,但是在瀏覽器中無法進行設置。


          在Browser的setting中添加設置User Agent的選項:

          1.在 res/xml/browser_preferences.xml中添加:

          <ListPreference
                          
          android:key="user_agent"
                          android:title
          ="@string/pref_user_agent"
                          android:defaultValue
          ="0"
                          android:entries
          ="@array/pref_development_ua_choices"
                          android:entryValues
          ="@array/pref_development_ua_values"
                          android:dialogTitle
          ="@string/pref_user_agent_dialogtitle" />


          2.在res/value/string.xml中添加對應的字符串,多語言的具體到不同的文件夾(自己添加)

          <string name="pref_user_agent">User agent</string>  
            
           
          <string name="pref_user_agent_dialogtitle"  translatable="false">User Agent</string>  
          <string-array name="pref_development_ua_choices" translatable="false">
                  
          <item>Android</item>
                  
          <item>Desktop</item>
                  
          <item>iPhone</item>
                  
          <item>iPad</item>
                  
          <item>Froyo-N1</item>
                  
          <item>NexusOne</item>
                  
          <item>Samsung i9000</item>
              
          </string-array>
              
          <!-- Do not tranlsate.  Development option -->
              
          <string-array name="pref_development_ua_values" translatable="false">
                  
          <item>0</item>
                  
          <item>1</item>
                  
          <item>2</item>
                  
          <item>3</item>
                  
          <item>4</item>
                  
          <item>5</item>
                  
          <item>6</item>
              
          </string-array>

          3. 在 src/com/android/BrowserSettings.java 中添加

          1. public final static String PREF_USER_AGENT = "user_agent";  

          4.在 src/com/android/BroserPreferencesPage.java 中添加
          private CharSequence getVisualUserAgent(String enumName) {
                  CharSequence result 
          = "";
                  CharSequence[] visualNames 
          = getResources().getTextArray(
                          R.array.pref_development_ua_choices);
                  CharSequence[] enumNames 
          = getResources().getTextArray(R.array.pref_development_ua_values);

                  
          if (visualNames.length == enumNames.length) {
                      
          for (int i = 0; i < enumNames.length; i++{
                          
          if (enumNames[i].equals(enumName)) {
                              result 
          = visualNames[i];
                          }

                      }

                  }

                  
          return result;
              }


          5. 在BroserPreferencesPage.java的OnCreate方法中添加

          = findPreference(BrowserSettings.PREF_USER_AGENT);  
              e.setOnPreferenceChangeListener(
          this);  
              e.setSummary(getVisualUserAgent(  
                  getPreferenceScreen().getSharedPreferences()  
                  .getString(BrowserSettings.PREF_USER_AGENT, 
          null))); 
           
          6.在BroserPreferencesPage.java的onPreferenceChange 方法中添加


          else if (pref.getKey().equals(BrowserSettings.PREF_USER_AGENT)){  
                  pref.setSummary(getVisualUserAgent((String)objValue));  
                  
          return true;  
              }
            

               
                7.在BrowserSettings.java的syncSharedPreferences方法中添加

          userAgent = Integer.parseInt(p.getString(PREF_USER_AGENT, "0"));


           

          posted @ 2011-10-18 12:51 天宇恒星 閱讀(4040) | 評論 (0)編輯 收藏

          if (Log.isLoggable(TAG, Log.DEBUG))  


          開日志級別的方法 

          第一種 : 

          C:\Users\alex>adb shell 
          # setprop log.tag.SQLiteQueryBuilder DEBUG 
          setprop log.tag.SQLiteQueryBuilder DEBUG 


          第二種: 

          第二個就在/data/local.prop文件里寫'log.tag.<YOUR_LOG_TAG>=<LEVEL>

          posted @ 2011-10-08 18:00 天宇恒星 閱讀(603) | 評論 (0)編輯 收藏


          android操作sim卡聯(lián)系人信息
          http://hi.baidu.com/yangduoliver/blog/item/709fbe019c9dc118738da523.html

          Android聯(lián)系人數(shù)據(jù)庫全解析
          http://jasonshieh.iteye.com/blog/793537

          Android管理聯(lián)系人(包含添加,查詢,修改和刪除;以及不同版本區(qū)別)
          http://blog.csdn.net/javatiger427/article/details/5969039

          Android中讀取電話本Contacts聯(lián)系人的所有電話號信息
          http://blog.csdn.net/wufenglong/article/details/5787486

          /Files/liuyanbo/聯(lián)系人總結.pdf

          posted @ 2011-09-25 22:45 天宇恒星 閱讀(171) | 評論 (0)編輯 收藏


          熟悉Windows編程的朋友可能知道Windows程序是消息驅動的,并且有全局的消息循環(huán)系統(tǒng)。而Android應用程序也是消息驅動的,按道理來說也應該提供消息循環(huán)機制。實際上谷歌參考了Windows的消息循環(huán)機制,也在Android系統(tǒng)中實現(xiàn)了消息循環(huán)機制。Android通過Looper、Handler來實現(xiàn)消息循環(huán)機制,Android消息循環(huán)是針對線程的(每個線程都可以有自己的消息隊列和消息循環(huán))。本文深入介紹一下Android消息處理系統(tǒng)原理。

              Android系統(tǒng)中Looper負責管理線程的消息隊列和消息循環(huán),具體實現(xiàn)請參考Looper的源碼。 可以通過Loop.myLooper()得到當前線程的Looper對象,通過Loop.getMainLooper()可以獲得當前進程的主線程的Looper對象。

              前面提到Android系統(tǒng)的消息隊列和消息循環(huán)都是針對具體線程的,一個線程可以存在(當然也可以不存在)一個消息隊列和一個消息循環(huán)(Looper),特定線程的消息只能分發(fā)給本線程,不能進行跨線程,跨進程通訊。但是創(chuàng)建的工作線程默認是沒有消息循環(huán)和消息隊列的,如果想讓該線程具有消息隊列和消息循環(huán),需要在線程中首先調用Looper.prepare()來創(chuàng)建消息隊列,然后調用Looper.loop()進入消息循環(huán)。如下例所示:

            class LooperThread extends Thread {
                public Handler mHandler;
          
                public void run() {
                    Looper.prepare();
          
                    mHandler = new Handler() {
                        public void handleMessage(Message msg) {
                            // process incoming messages here
                        }
                    };
          
                    Looper.loop();
                }
            }

          這樣你的線程就具有了消息處理機制了,在Handler中進行消息處理。

               Activity是一個UI線程,運行于主線程中,Android系統(tǒng)在啟動的時候會為Activity創(chuàng)建一個消息隊列和消息循環(huán)(Looper)。詳細實現(xiàn)請參考ActivityThread.java文件。

               Handler的作用是把消息加入特定的(Looper)消息隊列中,并分發(fā)和處理該消息隊列中的消息。構造Handler的時候可以指定一個Looper對象,如果不指定則利用當前線程的Looper創(chuàng)建。詳細實現(xiàn)請參考Looper的源碼。

               Activity、Looper、Handler的關系如下圖所示:

               一個Activity中可以創(chuàng)建多個工作線程或者其他的組件,如果這些線程或者組件把他們的消息放入Activity的主線程消息隊列,那么該消息就會在主線程中處理了。因為主線程一般負責界面的更新操作,并且Android系統(tǒng)中的weget不是線程安全的,所以這種方式可以很好的實現(xiàn)Android界面更新。在Android系統(tǒng)中這種方式有著廣泛的運用。

               那么另外一個線程怎樣把消息放入主線程的消息隊列呢?答案是通過Handle對象,只要Handler對象以主線程的Looper創(chuàng)建,那么調用Handler的sendMessage等接口,將會把消息放入隊列都將是放入主線程的消息隊列。并且將會在Handler主線程中調用該handler的handleMessage接口來處理消息。

               這里面涉及到線程同步問題,請先參考如下例子來理解Handler對象的線程模型:

          1、首先創(chuàng)建MyHandler工程。

          2、在MyHandler.java中加入如下的代碼:

          package com.simon;
          
          import android.app.Activity;
          import android.os.Bundle;
          import android.os.Message;
          import android.util.Log;
          import android.os.Handler;
          
          public class MyHandler extends Activity {
          	static final String TAG = "Handler";
          	Handler h = new Handler(){
              	public void handleMessage (Message msg)
              	{
              		switch(msg.what)
              		{
              		case HANDLER_TEST:
              			Log.d(TAG, "The handler thread id = " + Thread.currentThread().getId() + "\n");
              			break;
              		}
              	}
              };
          
          	static final int HANDLER_TEST = 1;
              /** Called when the activity is first created. */
              @Override
              public void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  Log.d(TAG, "The main thread id = " + Thread.currentThread().getId() + "\n");
          
                  new myThread().start();
                  setContentView(R.layout.main);
              }
          
              class myThread extends Thread
              {
              	public void run()
              	{
              		Message msg = new Message();
              		msg.what = HANDLER_TEST;
              		h.sendMessage(msg);
              		Log.d(TAG, "The worker thread id = " + Thread.currentThread().getId() + "\n");
              	}
              }
          }

          在這個例子中我們主要是打印,這種處理機制各個模塊的所處的線程情況。如下是我的機器運行結果:

          09-10 23:40:51.478: DEBUG/Handler(302): The main thread id = 1
          09-10 23:40:51.569: DEBUG/Handler(302): The worker thread id = 8
          09-10 23:40:52.128: DEBUG/Handler(302): The handler thread id = 1

          我們可以看出消息處理是在主線程中處理的,在消息處理函數(shù)中可以安全的調用主線程中的任何資源,包括刷新界面。工作線程和主線程運行在不同的線程中,所以必須要注意這兩個線程間的競爭關系。

               上例中,你可能注意到在工作線程中訪問了主線程handler對象,并在調用handler的對象向消息隊列加入了一個消息。這個過程中會不會出現(xiàn)消息隊列數(shù)據(jù)不一致問題呢?答案是handler對象不會出問題,因為handler對象管理的Looper對象是線程安全的,不管是加入消息到消息隊列和從隊列讀出消息都是有同步對象保護的,具體請參考Looper.java文件。上例中沒有修改handler對象,所以handler對象不可能會出現(xiàn)數(shù)據(jù)不一致的問題。

               通過上面的分析,我們可以得出如下結論:

          1、如果通過工作線程刷新界面,推薦使用handler對象來實現(xiàn)。

          2、注意工作線程和主線程之間的競爭關系。推薦handler對象在主線程中構造完成(并且啟動工作線程之后不要再修改之,否則會出現(xiàn)數(shù)據(jù)不一致),然后在工作線程中可以放心的調用發(fā)送消息SendMessage等接口。

          3、除了2所述的hanlder對象之外的任何主線程的成員變量如果在工作線程中調用,仔細考慮線程同步問題。如果有必要需要加入同步對象保護該變量。

          4、handler對象的handleMessage接口將會在主線程中調用。在這個函數(shù)可以放心的調用主線程中任何變量和函數(shù),進而完成更新UI的任務。

          5、Android很多API也利用Handler這種線程特性,作為一種回調函數(shù)的變種,來通知調用者。這樣Android框架就可以在其線程中將消息發(fā)送到調用者的線程消息隊列之中,不用擔心線程同步的問題。

               深入理解Android消息處理機制對于應用程序開發(fā)非常重要,也可以讓你對線程同步有更加深刻的認識。以上是最近Simon學習Android消息處理機制的一點兒總結,如有錯誤之處請不吝指教。

          posted @ 2011-09-20 14:56 天宇恒星 閱讀(334) | 評論 (0)編輯 收藏

          之前一直沒有搞懂a(chǎn)ndroid:padding和android:layout_margin的區(qū)別,其實概念很簡單,padding是站在父view的角度描述問題,它規(guī)定它里面的內容必須與這個父view邊界的距離。margin則是站在自己的角度描述問題,規(guī)定自己和其他(上下左右)的view之間的距離,如果同一級只有一個view,那么它的效果基本上就和padding一樣了。例如我的XML layout代碼如下:
           
          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:paddingLeft="10dip"
              android:paddingRight="10dip"
              android:paddingTop="10dip"
              android:paddingBottom="10dip"
              >
          <TextView  
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:background="#FF0000"
              android:text="@string/hello"
              android:paddingLeft="50dip"
              android:paddingRight="50dip"
              android:paddingTop="50dip"
              android:paddingBottom="50dip"
           android:layout_marginBottom="10dip"
              />
              <TextView  
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:background="#FF0000"
              android:text="@string/hello"
              android:paddingLeft="50dip"
              android:paddingRight="50dip"
              android:paddingTop="50dip"
              android:paddingBottom="50dip"
            android:layout_marginBottom="10dip"
              />
              <TextView  
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:background="#FF0000"
              android:text="@string/hello"
              android:paddingLeft="50dip"
              android:paddingRight="50dip"
              android:paddingTop="50dip"
              android:paddingBottom="50dip"
              android:layout_marginBottom="10dip"
              />
              <TextView  
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:background="#FF0000"
              android:text="@string/hello"
              android:paddingLeft="50dip"
              android:paddingRight="50dip"
              android:paddingTop="50dip"
              android:paddingBottom="50dip"
              android:layout_marginBottom="10dip"
              />
          </LinearLayout>

          posted @ 2011-09-20 14:51 天宇恒星 閱讀(214) | 評論 (0)編輯 收藏

          具體代碼如下:
          LOCAL_PATH := $(call my-dir)  
          include $(CLEAR_VARS)  
          LOCAL_STATIC_JAVA_LIBRARIES :
          = libarity  
          LOCAL_SRC_FILES :
          = $(call all-java-files-under, src)  
          LOCAL_PACKAGE_NAME :
          = TestJar  
          include $(BUILD_PACKAGE)  
          ##################################################  
          include $(CLEAR_VARS)  
          LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES :
          = libarity:lily.jar  
          include $(BUILD_MULTI_PREBUILT)  
          # Use the folloing include to make our test apk.  
          include $(call all
          -makefiles-under,$(LOCAL_PATH))  


          關鍵在于LOCAL_STATIC_JAVA_LIBRARIES := libarity和LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := libarity:lily.jar

          其實libarity是個名字可以隨便取,但是兩個屬性的值要一樣就好了,但是后面那個冒號里面就一定要寫你的jar包名,你的jar包一定放在工程的根目錄就好了,目錄結構如下:

          -res

          -src

          -com

          -ianc

          -testjar

          TestJar.java

          android.mk

          AndroidManifest.xml

          default.properties

          lily.jar


          posted @ 2011-09-20 14:49 天宇恒星 閱讀(1441) | 評論 (1)編輯 收藏

          字體大小

          對于能夠顯示文字的控件(如TextView EditText RadioButton Button CheckBox Chronometer等等),你有時需要控制字體的大小。Android平臺定義了三種字體大小。

          "?android:attr/textAppearanceLarge"

          "?android:attr/textAppearanceMedium"

          "?android:attr/textAppearanceSmall"

          使用方法為:

          android:textAppearance="?android:attr/textAppearanceLarge" 
          android:textAppearance="?android:attr/textAppearanceMedium" 
          android:textAppearance="?android:attr/textAppearanceSmall"

          style="?android:attr/textAppearanceLarge" 
          style="?android:attr/textAppearanceMedium" 
          style="?android:attr/textAppearanceSmall"

          字體顏色

          android:textColor="?android:attr/textColorPrimary" 
          android:textColor="?android:attr/textColorSecondary" 
          android:textColor="?android:attr/textColorTertiary" 
          android:textColor="?android:attr/textColorPrimaryInverse" 
          android:textColor="?android:attr/textColorSecondaryInverse"

          ProgressBar

          style="?android:attr/progressBarStyleHorizontal" 
          style="?android:attr/progressBarStyleLarge" 
          style="?android:attr/progressBarStyleSmall" 
          style="?android:attr/progressBarStyleSmallTitle"

          分隔符

          橫向:

          <View android:layout_width="fill_parent" 
                android:layout_height="1dip" 
                android:background="?android:attr/listDivider" />

          縱向:

          <View android:layout_width="1dip" 
                android:layout_height="fill_parent" 
                android:background="?android:attr/listDivider" />

          CheckBox

          style="?android:attr/starStyle"

          類似標題欄效果的TextView

          style="?android:attr/listSeparatorTextViewStyle"

          其它有用的樣式

          android:layout_height="?android:attr/listPreferredItemHeight"

          android:paddingRight="?android:attr/scrollbarSize"

          style="?android:attr/windowTitleBackgroundStyle"

          style="?android:attr/windowTitleStyle"

          android:layout_height="?android:attr/windowTitleSize"

          android:background="?android:attr/windowBackground"

          posted @ 2011-09-20 14:45 天宇恒星 閱讀(478) | 評論 (0)編輯 收藏

          僅列出標題
          共3頁: 上一頁 1 2 3 
          主站蜘蛛池模板: 木兰县| 保康县| 扎兰屯市| 安西县| 全州县| 巴东县| 固安县| 云阳县| 景宁| 松滋市| 太白县| 庆安县| 马尔康县| 高邑县| 莒南县| 保靖县| 南宁市| 增城市| 邵阳市| 县级市| 温泉县| 卢湾区| 罗城| 耿马| 丁青县| 咸宁市| 都昌县| 濮阳市| 武陟县| 邢台市| 吴川市| 崇信县| 盐源县| 平潭县| 苏尼特右旗| 阜康市| 普兰店市| 四子王旗| 信阳市| 华阴市| 黄大仙区|