Android老司機搬磚小技巧

來源:互聯網
上載者:User

標籤:split   can   cto   image   關注   ges   ddr   asc   guard   

作為一名Android世界的搬運工,每天搬磚已經夠苦夠累了,走在坑坑窪窪的道路一不小心就掉坑裡了。

SDK常用工具類

Android SDK中本身就擁有很多輪子,熟悉這些輪子,可以提高我們的搬磚效率。

  • android.text.TextUtils

    字串操作常用方法:isEmpty()join()split()

    if(!TextUtils.isEmpty(text)){    //do something}
  • android.webkit.URLUtil

    連結相關常用方法:isHttpUrl()isNetworkUrl()decode()

  • android.util.Patterns android.support.v4.util.PatternsCompat

    該類定義了許多常用Regex常量,可直接拿來用,例如:IP地址IP_ADDRESS,網址WEB_URL,Email地址EMAIL_ADDRESS,電話PHONE

    有了它,媽媽再也不用擔心我Regex的問題了。

    Matcher urlMatcher = Patterns.WEB_URL.matcher(text);if (urlMatcher.find()) {    //find url}
  • android.telephony.PhoneNumberUtils

    電話相關工具類

  • android.text.format.Formatter

    格式檔案大小,以GB,MB,KB為單位。

  • android.media.ThumbnailUtils

    建立圖片或影片縮圖

    常用方法:createImageThumbnail()createVideoThumbnail()

  • android.database.DatabaseUtils

    資料庫操作相關工具類

    ?

常見的坑

以下經驗是AC在開車時的出現事故的小筆記。其中的解決方案,由於飆車環境差異和AC本人的能力有限,各位老司機可以酌情參考,同時也歡迎老司機評論指出。

  1. Handler處理非同步作業時 用isFinishing()判斷當前的Activity是否存在: 比如在連網請求資料回調回來之後,在Handler的handleMessage()方法中,用

    isFinishing() 判斷當前的Activity是否存在,否則極易出現null 指標等異常。

  2. 在ListFragment中set footer,不要在onCreateView()中設定; 要在onActivityCreate()中設定 否則會Caused by: java.lang.IllegalStateException: Content view not yet created

  3. 如果頁面中的的樣式如果出現了很奇怪的渲染(花屏,閃爍等異常),看看所在的Activity或者View是否添加了硬體加速。

  4. setCustomAnimations方法要在replace,remove等方法之前,否則設定無效。

      
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();   ft.setCustomAnimations(R.anim.push_up_out, 0);   ft.replace(R.id.frame_content,mFragment);
     
  5. 在onCreate中擷取到View的高度和寬度,直接調用View.getHeight()View.getWidth(),均得到0。應該使用以下方式擷取View的高度或寬度。

    mView.post(()->{  Log.d("width:"+mView.getWidth()+",height:"+mView.getWidth());});
  6. ActivityA通過startActivityForResult()啟動了ActivityB。如果ActivityB的launchMode是singleTop或singleTask就要注意了,ActivityA的onActivityResult(int requestCode,int resultCode,Intent data)方法中的resultCode一定是Activity.RESULT_CANCELED

  7. startActivity的時候閃退並出現了以下log:

    • java.lang.SecurityException: Unable to find app for caller android.app.ApplicationThreadProxy。This log means that your app is having trouble handling a communication intent. Sometimes problems like this can be cause by sending an intent with big extras. Try to reduce the dimension of the image or, and this is the best way, save your image on the sd and put in the extras only the Uri of that image。
    • android.os.TransactionTooLargeException

    注意:不要在intent中傳遞大量的資料資料(例如List,大的圖片等),否則會拋異常。

  8. drawable 中的selector檔案 如果使用android:exitFadeDuration="@android:integer/config_shortAnimTime"這個屬性,那麼不同的手機可能會有不同的效果,慎用!!

  9. 在實現WebView與JS互動的過程中,如果遇到點擊後JS方法無響應,應該注意一下問題:(1)WebView.addJavascriptInterface(new AndroidClick(), "android");這個方法的別名android是否與JS中的對象名稱一致如 (2)WebView需要實現註冊這個回呼函數。

  10. WebView.setWebChromeClient(new WebChromeClient(){        @Override        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {            return super.onJsAlert(view, url, message, result);        } });

    (3)如果發布的APP有進行混淆,那麼AndroidClick這個JS 與 JAVA互動的類需要proguard.cfg檔案忽略這個類的混淆,否則混淆後JS將執行不了。

    keepclassmembers class net.angrycode.js2java.AndroidClick{  *;}
     
  11. TextView實現簡單的圖文,可以使用TextView的drawableTop等屬性來設定文本上的圖片;還可以配合ImageSpan使用來設定圖片。

  12. AlertDialog 按確定按鈕不消失的方法

AlertDialog dialog = builder.create();   dialog.setCanceledOnTouchOutside(true);   dialog.show();   dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> {           String text = editText.getText().toString().trim();           if (TextUtils.isEmpty(text)){               ToastUtils.showToast(this,R.string.say_something);               return;           }           //這裡執行對話方塊消失           dialog.dismiss();       });

 

關注我們,可以擷取更多

 

Android老司機搬磚小技巧

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.