Android 常用知識列舉

來源:互聯網
上載者:User

標籤:

  1. 在Activity可見時進行頁面重新整理,即處於Activity棧頂,當然,這種重新整理操作 可以在onStart,onResume,onRestart,onNewIntent+singleTask中,但最好的位置是onResume,其他方式可能會造成表單泄露

  2. Service的兩種啟動方式,startService和bindService,根據啟動方式的不同,當然也有一種稱為複合方式startService+bindService,Service銷毀的方式不同,startService可以做app退出後的後台服務,但bindService不行,startService+bindService也可以做後app退出後的後台服務,但app退出時必須unbind,反過來bindService+startService不能做app退出後的服務

  3. 建議在Activity增加一個變數activityIsActivity來判斷當前Activity是否處於活動狀態,Activity::onResume和Activity:onPause中改變其值,防止在無介面時發生表單泄露問題。

  4. 在Activity的onWindowFocusChanged的方法中監聽焦點的改變

  5. 使用InputMethodManager 退出鍵盤最靈活的方式是

    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);boolean isOpen=imm.isActive();//isOpen若返回true,則表示IME開啟if(isOpen){    imm.hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);      //(WidgetSearchActivity是當前的Activity)}

    當然,如果頁面只會有一個可以啟動鍵盤的EditText,可直接將MainActivity.this.getCurrentFocus()替換成該空間

  6. dip轉px的公式是 px = dip*scale+0.5f,scale是密度,當然,還可以使用  (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 20, context.getResources().getDisplayMetrics());

  7. 擷取DisplayMetrics的方式有2種,getWindow.getWindowManager.getDisplayMetrics(DisplayMetric dm),還有一種是context.getResources().getDisplayMetrics(),後者使用比較靈活,建議使用後者

  8. 自訂許可權+引用+使用自訂許可權的方式是,自訂<permission android:name="自訂許可權" android:protected="根據安全等級設定,如果保密性較高,使用簽名方式" />,然後引用<use-permission android:name="自訂許可權" />,使用自訂許可權 如下

    <receiver android:name=".SmsBroadCastReceiver" android:export="false" android:permission="自訂許可權">              <intent-filter android:priority="20">                  <action android:name="android.provider.Telephony.SMS_RECEIVED"/>              </intent-filter>          </receiver>
  9. android:export表示該組件是否被跨進程調用,預設是false,android:windowSoftInputMode表示設定 鍵盤彈出UI布局的改變 以及定義鍵盤彈出狀態,何時彈出等行為

  10. android:noHistory表示該組件不會在(最近開啟列表中)顯示,android:configChanges表示當發生指定的改變時,Activity程式不會重新啟動,響應的變化也會通過onConfigurationChanged中得到,android:screenOriention表示Activity應該處於何種螢幕狀態,android:lancheMode是指定建立模式

  11. 強制檢測跨進程調用的許可權問題

    public boolean isEnable(){    mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK,"you donot have the wake_lock permission");    Log.d("pid="+Binder.getCallingPid()+",uid="+Binder.getCallingUid());    return isEnableInternal();}


  12. <use-library>生命需要的串連共用庫,<use-feature>生命app所需要的硬體環境支援,如支援藍芽等,否則app將安裝失敗。

  13. android:clearTaskOnLaunch和android:finishOnTaskLauch的區別是,前者清楚自身以外的Activity,後者清除轉入背景Activity Task。後者比較常用。

  14. 系統會為每一個app分配一個虛擬機器進程,因此不想jvm所有的應用共用同一個虛擬機器進程

  15. 使用LocalBroadcastReceiver或者myView.getHandler進行訊息佇列方式的更新,自訂Handler時請以靜態方式繼承Handler,否則可能因為Handler持有的Activity無法銷毀,因為Handler本身是持有靜態對象,他的生命週期和app相同,因此為了讓Activity及時銷毀,必須靜態繼承 public static class MyHandler extends Handler


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.