Android 問題解決方法(一)


1、以使用android文件系統加載視頻文件

1)視頻文件存放在/res/raw下

2)

String videoName = "video";
int id = getResources().getIdentifier(videoName, "raw", getBaseContext().getPackageName());
final String path = "android.resource://" +getBaseContext().getPackageName() + "/" + id;
videoView.setVideoURI(Uri.parse(path));

2、Fragment傳參方法

·Fragment由系統控制,類中中不能寫構造函數,其傳入參數方法如下:

·用Bundle傳入參數

private static final String ARG_SECTION_NUMBER = "section_number";
public static HistoryListFragment newInstance(int sectionNumber) {
    RippleDrawable drawable ;
    HistoryListFragment fragment = new HistoryListFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    return fragment;
}

3、Context 儘量不要被定義爲靜態的

如果被定義成靜態的,要在onDestroy()置空,進行回收,避免內存溢出。

4、單例工具類

儘量不要寫把方法寫成靜態的,可以單提出寫一個工具類,工具類使用單例模式,保障內存中只有一個。

public static HistoryRecordUtils historyRecordUtils;
static ViewRecordUpdateBean viewRecordUpdateBean = ViewRecordUpdateBean.getInstance();
public synchronized static  HistoryRecordUtils getInstance(){
    if(historyRecordUtils==null){
        historyRecordUtils = new HistoryRecordUtils();
    }
    return historyRecordUtils;
}

5、Handler添加到MainLooper中

1)不帶參數
new Handler(Looper.myLooper()).post(new Runnable() {
    @Override
    public void run() {
        //在UI線程中處理 
    }
});

判斷一個線程是否是主線程:
Looper.getLooper() == Looper.getMainLooper()來判斷

2)帶參數
Looper myLooper, mainLooper;
myLooper= Looper.myLooper(); //獲得自己的Looper
mainLooper= Looper.getMainLooper(); //獲得自己的main的looper
private EventHandler mNoLooperThreadHandler = new EventHandler(mainLooper);
 class EventHandler extends Handler {  
 	public EventHandler(Looper looper) {  
  		super(looper);  
  	}  
  	public EventHandler() {  
  		super();  
 	}  
	public void handleMessage(Message msg) {  
  	// 可以根據msg.what執行不同的處理,這裏沒有這麼做 
		something... 
 	}  
 }  

6、INSTALL_FAILED_OLDER_SDK

Android apk運行所需要的最低版本高於你的真機的android版本。

在AndroidMainfest.xml中添加

 <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="16" />

7、獲得屏幕寬和高

    WindowManager windowManager = getWindowManager();
    Display display = windowManager.getDefaultDisplay();
    int screenWidth = display.getWidth();
    int screenHeight = display.getHeight();

8、Android Icon尺寸

LDPI (Low Density Screen,120 DPI),其圖標大小爲 36 x 36 px。
MDPI (Medium Density Screen, 160 DPI),其圖標大小爲 48 x 48 px。
HDPI (High Density Screen, 240 DPI),其圖標大小爲 72 x 72 px。
xhdpi (Extra-high density screen, 320 DPI),其圖標大小爲 96 x 96 px。
xxhdpi(xx-high density screen, 480 DPI),其圖標大小爲144 x 144 px。

9、代碼規範

    1. 所有的頁面承載是在 Fragment 中 而非 Activity 中。
    2. Fragment  Activity  中最好不要定義 public 方法互相調用。需要公用的東西提取到公共的工具類或者父類中。
    3. Activity Fragment 等 不要作爲參數隨意傳遞 需要 Context 的地方使用 getApplicationContext() ;
    4. 所有的數據庫調用最好封裝在 ContentProvider 中 ,避免多線程多進程操作同一個數據庫時產生異常。
    5. 空指針異常的判斷,在使用別人穿進的參數或者通過別人的方法取得返回值時,在無法確認參數一定不爲空的情況下,一定要進行空指針判斷。
    6. 注意 Handler 產生的內存泄漏 。
    7. 圖片加載使用 Fresco  Fresco 使用介紹 http://www.fresco-cn.org/

10、通知欄打通:

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
        setContentView(R.layout.activity_personal_detail);
//        try {
//
//            Method setM = Window.class.getDeclaredMethod("setDarkStatusIcon",
//                    boolean.class);
//
//            setM.invoke(getWindow(), true);
//
//        } catch (Exception e) {
//
//        }

    }

11、清除callback方法:

private void clearTouchHelper() {
//mItemTouchHelper是 implements RecyclerView.OnChildAttachStateChangeListener
        if (mItemTouchHelper != null) {
            Class clazz = mItemTouchHelper.getClass();
            try {
                Method destroyCallbacks = clazz.getDeclaredMethod("destroyCallbacks");
                destroyCallbacks.setAccessible(true);
                destroyCallbacks.invoke(mItemTouchHelper);  //調用清理方法
                mItemTouchHelper = null;
            } catch (Exception e) {
                e.printStackTrace();
            }


        }
    }





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章