Android细节问题总结(二)
這篇博客是用來記錄自己在寫代碼的過程中遇到的一些問題,以及解決方法,做一個總結,算是筆記吧。
1.問題描述:
以某一觸發(fā)喚醒屏幕
解決方案:
public static void wakeUpAndUnlock(Context context){KeyguardManager km= (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");// 解鎖kl.disableKeyguard();// 獲取電源管理器對象PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE);// 獲取PowerManager.WakeLock對象,后面的參數|表示同時傳入兩個值,最后的是LogCat里用的TagPowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");// 點亮屏幕wl.acquire();// 釋放wl.release();} 注:其中,解鎖步驟可以省略。這種情況下,就只是點亮你的屏幕而不會解鎖了。
2.問題描述:
除掉Actionbar最左側的箭頭
注意,這個最左側的的箭頭可不是actionbar的icon。如果你去設置它的icon,這樣就會導致既有icon也有箭頭。
解決方案:
getActionBar().setHomeAsUpIndicator(drawable); 上面的這種方法是適用于API18,而對于API小于18的代碼則是不適用的。需要做更多的邏輯。如下:
public static void changeActionBarHomeUpDrawable(Activity activity, int rid) {Drawable homeUp = activity.getResources().getDrawable(rid);final View home = activity.findViewById(android.R.id.home);if (home == null) {// Action bar doesn't have a known configuration, an OEM messed with things.return;}final ViewGroup parent = (ViewGroup) home.getParent();final int childCount = parent.getChildCount();if (childCount != 2) {// No idea which one will be the right one, an OEM messed with things.return;}final View first = parent.getChildAt(0);final View second = parent.getChildAt(1);final View up = first.getId() == android.R.id.home ? second : first;if (up instanceof ImageView) {ImageView upIndicatorView = (ImageView) up;upIndicatorView.setImageDrawable(homeUp);}}
3.問題描述:
Android Private Libraries缺失
如果你的Android Private Libraries是你誤刪除的話,你可以通過下面的方法1恢復。如果你的項目無原無故的缺失了Android Private Libraries,那么你可以使用下面的方法2修復。
1. 在項目上點擊右鍵,點擊Android Tools -> Fix Project Properties 即可
2. android-support-v4重復,刪除一個即可
4.問題描述:
Arrays.copyOfRange在低版本的Android中出現NoSuchMethodError異常
對于這個問題的解釋,可能要歸結于API版本之間的兼容性了。
解決方案:
我的解決方法就是直接重寫了這個方法。非常簡單粗暴的一個方法,而且很有效。下面是對于Array的一些常用方法的示例源碼:
http://commons.apache.org/proper/commons-lang/apidocs/src-html/org/apache/commons/lang3/ArrayUtils.html
5.問題描述:
ListView與動態(tài)顯示的底部操作欄
你是否遇到過這樣的一個問題:你有一個始終位于屏幕底部的Layout,還有一個ListView,它是始終位于屏幕的頂部。當ListView的item過多時,下面的部分item就會被攔住,或是把底部的Layout攔住。
解決方案(一):
對于上面的問題,可能你已經嘗試過,使用讓Listview位于Layout上方,或是ListView同時位于屏幕頂部和Layout上方。可是嘗試過后就會是沮喪的。下面我就分享一個我的技巧:在Layout的外部再套一層Layout,高度為wrap_content。如下:
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><ListViewandroid:id="@+id/listView1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_above="@+id/container_relativeLayout"android:layout_alignParentTop="true"android:layout_centerHorizontal="true" ></ListView><RelativeLayoutandroid:id="@+id/container_relativeLayout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_horizontal" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Bottom Layout" /></LinearLayout></RelativeLayout></RelativeLayout>
注:可能你會覺得這樣是多此一舉。因為我可以把ListView的高度設置成match_parent或是fill_parent,再位于Layout之上。這種方式只是適合于,Layout不會被Gone的情況。這里是一個值得注意的地方。
解決方案(二):
可能你會覺得方案一太過繁瑣了,那么你可以使用Relayout的另一個屬性:layout_alignWithParentIfMissing
android:layout_alignWithParentIfMissing="true"
6.問題描述:
has leaked window com.android.internal.policy.impl.PhoneWindow$ that was originally added here
大致的意思是存在窗口句柄泄露,即未能及時銷毀某個PhoneWindow。其實存在這么一種情況,即因我們在非主線程中的某些操作不當而產生了一個嚴重的異常,從而強制當前Activity被關閉。而在關閉的同時,卻沒能及時的調用dismiss來解除對ProgressDialog等的引用,從而系統拋出了標題中的錯誤,而掩蓋了真正導致這個錯誤的異常信息。
解決方案:
重寫Activity/Fragment的onDestroy方法,在方法中調用dismiss來解除對ProgressDialog等的引用。
7.問題描述:
Android判斷是否能真正上網
普通方法不能判斷外網的網絡是否連接,比如連接上局域網
解決方案:
public static final boolean ping() {String result = null;try {String ip = "www.baidu.com"; // ping 的地址,可以換成任何一種可靠的外網Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip); // ping網址3次// 讀取ping的內容,可以不加InputStream input = p.getInputStream();BufferedReader in = new BufferedReader(new InputStreamReader(input));StringBuffer stringBuffer = new StringBuffer();String content = "";while ((content = in.readLine()) != null) {stringBuffer.append(content);}// ping的狀態(tài)int status = p.waitFor();if (status == 0) {result = "success";return true;} else {result = "failed";}} catch (IOException e) {result = "IOException";} catch (InterruptedException e) {result = "InterruptedException";} finally {Log.d("----result---", "result = " + result);}return false;}
8.問題描述:
獲取手機號碼
解決方案:
private String getPhoneNumber() {TelephonyManager telephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); return telephonyMgr.getLine1Number(); } 添加權限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
9.問題描述:
lost?serialVersionUID
在開發(fā)過程中,有時我們會遇到“The serializable class CommonException does not declare a static final serialVersionUID field of type long”這樣的一個報警異常。類似下面這樣的:
我們需要這個值的原因是我們要把這個值永久地保存在一個磁盤上,通常是一個文件中。還有一點是我們要把這個對象在網絡中傳輸。
解決方案:
add上面的3 quick fixes available中的一個。通常是去add第二個。
10.問題描述:
如何隱藏底部狀態(tài)欄(System Bar)
底部導航欄如下:
隱藏方式:
在onCreate中添加如下代碼:
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
11.問題描述:
獲得屏幕在旋轉過程中的旋轉角度
這里我們獲得的不只是橫屏還是豎屏的問題。而是更細一點的區(qū)分,比如橫屏中的是和左旋轉了90度,還是向右旋轉了90.解決方案如下:
WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); mRotation = manager.getDefaultDisplay().getRotation();
12.問題描述:
Installation error: INSTALL_FAILED_VERSION_DOWNGRADE
? 安裝新的Apk時,出現以上報錯信息。這是是因為Manifest中的android:versionCode屬性,手機里APP的versionCode高于將要安裝的APP。
解決方案:
? 1.卸載原來的Apk
? 2.將當前的Apk的versionCode屬性值修改到比手機中Apk的versionCode屬性值大即可。
大家還可以參見一些Android其他的細節(jié)總結:
Android細節(jié)問題總結(一)
總結
以上是生活随笔為你收集整理的Android细节问题总结(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android退出程序(三)——Andr
- 下一篇: Android新控件RecyclerVi