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.  

会持续更新......................



本文转自 lingdududu 51CTO博客,原文链接http://blog.51cto.com/liangruijun/722171

上一篇:dedecms织梦建站后怎么防止被黑,加强安全漏洞措施?


下一篇:不止GitHub,阿里云也能托管博客源代码!