Android 心得不斷更新中

來源:互聯網
上載者:User

1.每個Activity都有一個自己的window:
  在activity的onCreate方法中,調用setContentView方法,其調用的是getWindow().setContentView()方法。
而getWindow()返回的Window 對象其實是Window抽象類別的子類PhoneWindow.
mWindow = PolicyManager.makeNewWindow(this);
該句code是在Activity的attach()方法中調用。
2.在UI線程中調用invalidate()方法,即執行重新整理操作—調用view 的onDraw()方法,但是如果在UI的子線程中,需要使用postInvalidate()方法。
3.應用程式寫日誌的Tag擷取,如下:
private static String TAG = CameraTestActivity.class.getSimpleName();
4.java方法中可變參數支援,如下例子:
 
[java] 
 private static String findSettableValue(Collection<String> supportedValues, 
                                          String... desiredValues) { 
   String result = null; 
    if (supportedValues != null) { 
      for (String desiredValue : desiredValues) { 
        if (supportedValues.contains(desiredValue)) { 
          result = desiredValue; 
          break; 
        } 
      } 
    } 
    return result; 
  } 
//調用: 
1)  
      flashMode = findSettableValue(parameters.getSupportedFlashModes(), 
                                    Camera.Parameters.FLASH_MODE_TORCH, // string 常量"torch" 
                                    Camera.Parameters.FLASH_MODE_ON);      // string常量"on" 
2) 
      flashMode = findSettableValue(parameters.getSupportedFlashModes(), 
                                    Camera.Parameters.FLASH_MODE_OFF);    // string常量"off" 
5.分享介面:
[java]
Intent i=new Intent(Intent.ACTION_SEND);    
i.setType("text/plain");    
i.putExtra(Intent.EXTRA_SUBJECT,"這裡是標題");    
i.putExtra(Intent.EXTRA_TEXT, "這裡是分享內容");     
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    
startActivity(Intent.createChooser(i, "分享"));  
6.使用應用程式套件組合名啟動外部應用:
[java] 
//1)使用Intent的setComponent方法 
Intent intent = new Intent(); 
intent.setComponent(new ComponentName(“包名”, “包名.主類名”)); 
intent.setAction(Intent.ACTION_VIEW); 
startActivity(intent); 
//2)使用包管理器 
Intent intent = new Intent(); 
intent = getPackageManager().getLaunchIntentForPackage(“包名”); 
startActivity(intent); 
7.使用Collections介面進行排序:
[java] 
//1)定義一個排序的類: 
  private static class ByFirstStringComparator implements Comparator<String[]>, Serializable { 
    @Override 
    public int compare(String[] o1, String[] o2) { 
      return o1[0].compareTo(o2[0]); 
    } 
  } 
 
//2)使用: 
 Collections.sort(listArray, new ByFirstStringComparator()); 


8.再按一次後退鍵退出應用程式:
[java] 
private static Boolean isExit = false; 
    private static Boolean hasTask = false; 
    Timer tExit = new Timer(); 
    TimerTask task = new TimerTask() { 
          
        @Override 
        public void run() { 
            isExit = false; 
            hasTask = true; 
        } 
    }; 
      
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        System.out.println("TabHost_Index.java onKeyDown"); 
        if (keyCode == KeyEvent.KEYCODE_BACK) { 
            if(isExit == false ) { 
                isExit = true; 
                Toast.makeText(this, "再按一次後退鍵退出應用程式", Toast.LENGTH_SHORT).show(); 
                if(!hasTask) { 
                    tExit.schedule(task, 2000); 
                } 
            } else { 
                finish(); 
                System.exit(0); 
            } 
        } 
        return false; 
    } 


9.開啟和關閉Android APN網路,即存取點設定
 

10.eclipse xml 編輯設定:
1)修改Eclipse的XML格式化配置
    這一步的配置是使格式化的效果為控制項的每個屬性配置佔一行。進入 Window/Preferences,展開到 XML/XML Files/Editor,
勾選 Split multiple attributes each on a new line
經此配置後,每次使用快速鍵  Ctrl+Shift+F 鍵格式化後每個屬性配置就會佔一行。

2)壓縮節點的聲明方式
    這步的目的是將沒有子節點的元素的聲明方式進行壓縮,如將 <TextView ...></TextView>轉化為  <TextView .../>。
方法為在XML檔案內空白地方點擊滑鼠右鍵,選擇 Source/Cleanup Document...

11.殺死後台進程
[java] 
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);  
activityManager.killBackgroundProcesses("name.of.your.package");  
需要一個許可權
 KILL_BACKGROUND_PROCESSES

12.擷取手機型號和系統版本號碼
[java] 
<span style="color:#cccccc;"></span><pre name="code" class="java">String sdk=android.os.Build.VERSION.SDK;// SDK號 
String model=android.os.Build.MODEL;   // 手機型號 
String release=android.os.Build.VERSION.RELEASE;</pre><pre name="code" class="java">// android系統版本號碼</pre> 
<pre></pre> 
<pre name="code" class="java"><pre name="code" class="java">// 擷取手機的DeviceID,即手機的唯一標識。 
// GSM網路:IMEI號 
// ESN,CDMA網路:MEID號 
// 擷取不到時,返回null 
// 需要許可權:READ_PHONE_STATE 
TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); 
String deviceId = tm.getDeviceId(); 
</pre> 
<pre></pre> 
<pre></pre> 
<pre></pre> 
<pre></pre> 
<pre></pre> 
</pre> 
13.使用Handler發送訊息:
[java] 
1) 建立Handler: 
 
Handler mHandler = new Handler(){ 
 
     public void handleMessage(Message msg){ 
 
        swtich(msg.what){ 
 
             case msgId: 
 
                  // 訊息為msgId的處理 
 
               break; 
 
            case msgId2: 
 
                // 訊息為msgId2的處理 
 
               break; 
 
           default: 
 
               break; 
 
      } 
 
     } 
 

 
2) 程式中發送訊息: 
 
Message m = new Message(); 
 
m.what=msgid; 
 
handler.sendMessage(m); 
 
如果需要傳遞資料,可以使用如下: 
 
Bundle b = new Bundle(); 
 
b.putString(key,value); 
 
b.putString(key1,value2); 
 
m.setData(b); 
 
handler.sendMessage(b); 
14.使用SharePreferences 儲存資料:
[java] 
1) 從檔案中擷取值: 
// 得到SharePreferences對象,每個相同的sharedFileName返回的是相同的對象,即單例模式。 
//0:Context.MODE_PRIVATE 
//1:Context.MODE_WORLD_READABLE 
//2:Context.MODE_WORLD_WRITEABLE 
//3:Context.MODE_APPEND 
//4:Context.MODE_MULTI_PROCESS 
//8:Context.MODE_ENABLE_WRITE_AHEAD_LOGGING 
SharedPreferences configSharedPref = mContext.getSharedPreferences("sharedFileName",0); 
String  str = configSharedPref.getString("key","defaultValue"); 
2) 修改儲存的資料: 
SharePreferences.Editor configEditor = configSharedPref.edit(); 
configEditor.putString("key","new value"); 
configEdit.commit(); 
15.建立案頭捷徑:
[java] 
Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name)); 
shortcutIntent.putExtra("duplicate", false); 
 
Intent appIntent = new Intent(mContext, MainActivity.class); 
appIntent.setAction("android.intent.action.MAIN"); 
appIntent.addCategory("android.intent.category.LAUNCHER"); 
 
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, appIntent); 
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(mContext, R.drawable.icon); 
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,iconRes); 
sendBroadcast(shortcutIntent); 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.