Android開發中一些疑難異常解決

Android開發中總是會遇到一些奇奇怪怪的問題,有時真不知如何下手。點滴經驗,重在積累。分享我在實際開發中遇到的一些問題,以後還會繼續添加或者有單獨篇幅進行分析。

java.lang.SecurityException: Not allowed to bind to service Intent解決

Android開發過程中,有時候需要在應用中創建遠程服務,以供其他應用調用,比如藍牙服務,WIFI服務等等。那麼,在開發過程中,會遇到java.lang.SecurityException: Not allowed to bind to service Intent綁定服務失敗的問題,依次按照下面的過程即可解決:
1、manifest文件中的service聲明加屬性:android:process=”:remote”
android:exported=”true”,並且要添加的action。

<service android:name=”com.idengpan.exserv.RemoteService” android:process=”:remote”
android:exported=”true” android:permission=”android.permission.INTERNET”>
<intent-filter >
<action android:name=”com.idengpan.exserv.RemoteService” android:exported=”true” />
</intent-filter>
</service>

2、第一種方式不湊效,嘗試卸載2個應用,重新安裝再啓動服務試試。因爲可能有服務正在運行的情況。
3、以上2種均不湊效,就要看遠程服務的配置裏是否配置了 android:permission=“xxxx”的權限,那麼在調用服務的應用中要申明這樣的權限纔可以調用,否則也會綁定失敗。
以上是本人在demo中遇到的問題並行之有效的方法,不保證能囊括所有的情況。

Exported service does not require permission配置問題

在Android開發過程中,service是比較常用的組件。作爲四大組件之一,service需要在manifest文件中進行申明註冊。和activity方式一樣,類似的格式。
但在申明的過程中,總是會遇到一些很奇怪的警告,儘管這些警告對程序運行沒什麼影響,但如果可以的話我們還是希望將警告消滅掉。Exported service does not require permission就是其中一個錯誤。其申明的格式爲:

<service android:name=”com.idengpan.exserv.MyService”>
<intent-filter >
<action android:name=”com.idengpan.exserv.MyService”/>
</intent-filter>
</service>

簡單的service聲明以及action註冊。
第一種解決方案是根據警告解決添加一個permission既可。如下

<service android:name=”com.idengpan.exserv.MyService” android:permission=”android.permission.INTERNET”>
<intent-filter >
<action android:name=”com.idengpan.exserv.MyService”  />
</intent-filter>
</service>

第二種解決方案是加入exported=”false”即可,exported屬性指該服務是否能夠被其他應用程序組件調用或跟它交互,注意是在service節點以及action節點都添加。如下:

<service android:name=”com.idengpan.exserv.MyService”
android:exported=”false”>
<intent-filter >
<action android:name=”com.idengpan.exserv.MyService” android:exported=”false” />
</intent-filter>
</service>

這樣警告即可消除了。

Android Dialog requestFeature() must be called before adding content

Exception: requestFeature() must be called before adding content的錯誤,根據錯誤原因,知道通過window什麼的來設置自定義的效果之前,需要加入一些內容View。所以,我們在調用setContentView方法前,就需要完成所有的動作。一般的解決方案是在dialog.show()後調用dialog.setContentView方法,這樣就可以好好的解決了!

相機連拍Camera.open返回空如何解決

最近開發android應用用到相機,因爲要連拍,需要自定義相機,然後一整套的代碼都需要寫了。但寫到開始的打開相機以及設置其屬性時,連拍模式下的打開相機服務總是會報異常並將app退出。Camera.open();這一句,很蛋疼。不管怎麼在surfaceDestroyed()方法和onDestroy()方法中執行Camera對象的釋放

if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}

都無法解決。後來才找到這種釋放代碼要放到onPause中才行……真心蛋疼。但總算是解決問題了。

library工程No resource identifier found for attribute

自定義了View,也定義了對應的declare-styleable屬性值,但是在編譯運行時,主工程會報錯No resource identifier found for attribute “XXX”這種惱人的問題。
這種問題的原因是:在定義自定義View的佈局文件裏,名稱空間要寫主工程的包名,而不能寫library工程(也就是被引用工程的包名)。那麼問題來了,如果這樣,library就失去了意義,因爲你被某一個app(包名)定義了,不能再擴展。最終的解決方案是:xmlns:yf=”http://schemas.android.com/apk/res/com.yuanfang.xxx”改成
xmlns:yf=”http://schemas.android.com/apk/res-auto”。完美解決!

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