Android開發學習筆記:Android很有用的代碼片段

  1. 1:查看是否有存儲卡插入  
  2. String status=Environment.getExternalStorageState();  
  3. if(status.equals(Enviroment.MEDIA_MOUNTED)){  
  4.    說明有SD卡插入  
  5. }  
  6.  
  7. 2:讓某個Activity透明  
  8. OnCreate 中不設Layout  
  9. this.setTheme(R.style.Theme_Transparent);  
  10. 以下是 Theme_Transparent的定義(注意transparent_bg是一副透明的圖片)  
  11.  
  12. 3:在屏幕元素中設置句柄  
  13. 使用Activity.findViewById來取得屏幕上的元素的句柄. 使用該句柄您可以設置或獲取任何該對象外露的值.  
  14. TextView msgTextView = (TextView)findViewById(R.id.msg);  
  15.    msgTextView.setText(R.string.push_me);  
  16.  
  17. 4:發送短信  
  18.  
  19.             String body=”this is mms demo”;  
  20.  
  21.            Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(”smsto”, number, null));  
  22.            mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);  
  23.            mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, true);  
  24.            mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, true);  
  25.             startActivity(mmsintent);  
  26.  
  27.    5:發送彩信  
  28.  
  29.            StringBuilder sb = new StringBuilder();  
  30.  
  31.             sb.append(”file://”);  
  32.  
  33.             sb.append(fd.getAbsoluteFile());  
  34.  
  35.             Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(”mmsto”, number, null));  
  36.             // Below extra datas are all optional.  
  37.             intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_SUBJECT, subject);  
  38.             intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);  
  39.             intent.putExtra(Messaging.KEY_ACTION_SENDTO_CONTENT_URI, sb.toString());  
  40.             intent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, composeMode);  
  41.             intent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, exitOnSent);  
  42.  
  43.             startActivity(intent);  
  44.  
  45. 7:發送Mail  
  46.  
  47.              mime = “img/jpg”;  
  48.             shareIntent.setDataAndType(Uri.fromFile(fd), mime);  
  49.             shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fd));  
  50.             shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);  
  51.  
  52.             shareIntent.putExtra(Intent.EXTRA_TEXT, body);  
  53.  
  54. 8:註冊一個 BroadcastReceiver  
  55.  
  56. registerReceiver(mMasterResetReciever, new IntentFilter(”oms.action.MASTERRESET”));  
  57.  
  58. private BroadcastReceiver mMasterResetReciever = new BroadcastReceiver() {  
  59.  
  60.         public void onReceive(Context context, Intent intent){  
  61.             String action = intent.getAction();  
  62.             if(”oms.action.MASTERRESET”.equals(action)){  
  63.                 RecoverDefaultConfig();  
  64.             }  
  65.         }  
  66.  
  67.     };  
  68.  
  69. 9:定義ContentObserver,監聽某個數據表  
  70.  
  71. private ContentObserver mDownloadsObserver = new DownloadsChangeObserver(Downloads.CONTENT_URI);  
  72.  
  73. private class DownloadsChangeObserver extends ContentObserver {  
  74.         public DownloadsChangeObserver(Uri uri) {  
  75.             super(new Handler());  
  76.  
  77.         }  
  78.  
  79.         @Override 
  80.         public void onChange(boolean selfChange) {}    
  81.         }  
  82.      
  83.  
  84. 10:獲得 手機UA  
  85.  
  86. public String getUserAgent(){  
  87.     String user_agent = ProductProperties.get(ProductProperties.USER_AGENT_KEY, null);  
  88.             return user_agent;  
  89.     }  
  90.  
  91. 11:清空手機上Cookie  
  92.  
  93. CookieSyncManager.createInstance(getApplicationContext());  
  94.         CookieManager.getInstance().removeAllCookie();  
  95.  
  96. 12:建立GPRS 連接  
  97.  
  98.    //Dial the GPRS link.  
  99.     private boolean openDataConnection() {  
  100.         // Set up data connection.  
  101.         DataConnection conn = DataConnection.getInstance();       
  102.  
  103.             if (connectMode == 0) {  
  104.                 ret = conn.openConnection(mContext, “cmwap”, “cmwap”, “cmwap”);  
  105.             } else {  
  106.                 ret = conn.openConnection(mContext, “cmnet”, “”, “”);  
  107.             }  
  108.  
  109.     }  
  110.  
  111. 13:PreferenceActivity 用法  
  112.  
  113. public class Setting extends PreferenceActivity{  
  114.     public void onCreate(Bundle savedInstanceState) {  
  115.         super.onCreate(savedInstanceState);  
  116.         addPreferencesFromResource(R.xml.settings);  
  117.     }  
  118.  
  119. }  
  120.  
  121. Setting.xml:  
  122.  
  123.             android:key=”seting2″  
  124.             android:title=”@string/seting2″  
  125.             android:summary=”@string/seting2″/>  
  126.  
  127.             android:key=”seting1″  
  128.             android:title=”@string/seting1″  
  129.             android:summaryOff=”@string/seting1summaryOff”  
  130.             android:summaryOn=”@stringseting1summaryOff”/>  
  131.  
  132. 14:通過 HttpClient從指定server獲取數據  
  133.  
  134.              DefaultHttpClient httpClient = new DefaultHttpClient();  
  135.             HttpGet method = new HttpGet(“http://www.baidu.com/1.html”);  
  136.             HttpResponse resp;  
  137.             Reader reader = null;  
  138.             try {  
  139.                 // AllClientPNames.TIMEOUT  
  140.                 HttpParams params = new BasicHttpParams();  
  141.                 params.setIntParameter(AllClientPNames.CONNECTION_TIMEOUT, 10000);  
  142.                 httpClient.setParams(params);  
  143.                 resp = httpClient.execute(method);  
  144.                 int status = resp.getStatusLine().getStatusCode();  
  145.  
  146.                 if (status != HttpStatus.SC_OK) return false;  
  147.  
  148.                 // HttpStatus.SC_OK;  
  149.                 return true;  
  150.             } catch (ClientProtocolException e) {  
  151.                 // TODO Auto-generated catch block  
  152.                 e.printStackTrace();  
  153.             } catch (IOException e) {  
  154.                 // TODO Auto-generated catch block  
  155.                 e.printStackTrace();  
  156.             } finally {  
  157.                 if (reader != nulltry {  
  158.                     reader.close();  
  159.                 } catch (IOException e) {  
  160.                     // TODO Auto-generated catch block  
  161.                     e.printStackTrace();  
  162.                 }  
  163.             }  
  164.  
  165. 15:顯示toast  
  166. Toast.makeText(this._getApplicationContext(), R.string._item, Toast.LENGTH_SHORT).show();  
  167.  
  168. 16:在當前Activity中啓動另外一個Activity  
  169. startActivity(new Intent(this,目標Activity.class));  
  170.  
  171. 17:從當前ContentView從查找控件  
  172. (Button)findViewById(R.id.btnAbout)      
  173.  R.id.btnAbout指控件id。  
  174.  
  175. 18:獲取屏幕寬高  
  176. DisplayMetrics dm = new DisplayMetrics();    
  177. //獲取窗口屬性  
  178.  getWindowManager().getDefaultDisplay().getMetrics(dm);      
  179.  int screenWidth = dm.widthPixels;//320    
  180.  int screenHeight = dm.heightPixels;//480   
  181.  
  182. 19:無標題欄、全屏  
  183. //無標題欄    
  184. requestWindowFeature(Window.FEATURE_NO_TITLE);    
  185. //全屏模式    
  186. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
  187.   WindowManager.LayoutParams.FLAG_FULLSCREEN);   
  188. 注意在setContentView()之前調用,否則無效。  
  189.  
  190. 20註冊activity  
  191. 所有用到的Activity都必須在AndroidManifest.xml中註冊,否則會報空指針錯誤。  
  192. 如:,注意是包名+類名。  
  193.  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章