android 開發中常見問題

smilestone322發表於2015-12-05

1)ActivityManager: Warning: Activity not started, its current task has been brought to the front
原因是:兩個應用起了同樣的名字,模擬器總是識別第一個解決方法是:重新啟動一邊模擬器;如果還是不行,可以先把模擬器delete掉再重新add就可以了.

2)Failed to install HelloWorld.apk on device timeout

原因:

由於模擬器已經開啟而沒有關閉或者非法關閉引起的。

解決方法:

刪除 C:\Documents and Settings\Administrator\.android\avd\對應版本.avd
下所有以.lock結尾的資料夾。
http://stackoverflow.com/questions/5994026/failed-to-install-helloandroid-apk-on-device-emulator-5554
 
3)check all projects
檢查所有的專案?

4)使用android模擬器,怎麼老是顯示android的啟動畫面,進不去?
換成android 4.2(17)的api就ok;


5)Eclipse如何開啟Package Explorer?
用以下方法開啟:
Window ----- Show View ---Package Explorer


6)   在使用eclipse的在佈局檔案時,有時編輯一些控制元件的字串名字中,有時會提示諸如“Hardcoded string "下一步", should use @string resource”的警告,這是什麼原因呢?

應該在res/values/strings.xml中設定:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="message">Button1</string>
</resources>
引用的時候使用:android:text="@string/message"就行了。


7)Not targeting the latest versions of Android; compatibility modes apply. Consider testing and updating this
 version. Consult the android.os.Build.VERSION_CODES javadoc for details.

Android Runtime和Dalvik會根據target SDK version決定是否工作在『相容模式』下,所謂相容模式,就是關閉了新版本中各種新機制和體驗優化的狀態。targetSdkVersion如果設定很低,

就等於是關閉了所有高版本的新特性和機制,包括『螢幕自適應』、『硬體加速』。
為了保證各個版本的相容性,及時使用到新特性,

targetSdkVersion因隨Android最新版本的釋出而持續提高,以保證在各個Android版本的裝置上都能獲得完整的體驗。


8)Unexpected text found in layout file: """

答覆:Ctrl+Shift+F ,刪掉多餘的字元;


9)Can't bind to local 8700 for debugger

Set Base local debugger port to "8601"
Check the box that says "Use ADBHOST" and the value should be "127.0.0.1"


10)android Uri.parse Unfortunately has stop
12-04 09:51:38.369: E/AndroidRuntime(824): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=Intent.ACTION_DIAL dat=tel:xxxxxx }


開始以為是許可權不對,在AndroidManifest中增加了許可權,還是不行;
<uses-permission id="android.permission.CALL_PHONE" />

將程式碼修改成這樣;

 //Intent intent=new Intent("Intent.ACTION_DAIL");
 Intent intent=new Intent("android.intent.action.CALL");
 intent.setData(Uri.parse("tel:10086"));
 startActivity(intent);

解決問題,同樣,如果要開啟網頁,使用以下程式碼:

Intent intent=new Intent("android.intent.action.VIEW");
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);

 


11)ActivityManager: Warning: Activity not started, its current task has been brought to the front
project->clean

 

12)如何在android模擬器中解除安裝已經安裝的應用?
通過android 模擬器的圖形介面解除安裝,或者通過adb解除安裝;

13)ContentProvier使用時:java.lang.SecurityException: Permission Denial:XXX解決辦法 .

解決辦法 ,在AndroidManifest.xml里加入android:exported = "true"

        <provider
            android:name="com.example.databasetest.DatabaseProvider"
            android:authorities="com.example.databasetest.provider"
            android:exported = "true" >
        </provider>

14)getSystemService 函式和AlarmManager 類的作用

 


15)通知:android logcat裡面AndroidRuntime FATAL EXCEPTION: main
java.lang.SecurityException: Requires VIBRATE permission

需要加上許可權:
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>


16)The constructor Notification(int, CharSequence, long) is deprecated

The constructor was deprecated in api level 11. so you should use Notification.Builder.


 
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Notification.Builder builder = new Notification.Builder(MainActivity.this); 
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
   
//設定通知屬性
builder.setContentIntent(pi);            
builder.setSmallIcon(R.drawable.ic_launcher);// 設定圖示            
builder.setWhen(System.currentTimeMillis());// 設定通知來到的時間 
          
// builder.setAutoCancel(true);           
builder.setContentTitle("標題");// 設定通知的標題            
builder.setContentText("內容");// 設定通知的內容           
builder.setTicker("狀態列上顯示");// 狀態列上顯示           
builder.setOngoing(true);       
builder.setSound(Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "5")); 
   
// builder.setVibrate(new long[]{2000,1000,4000});           
Notification notification = builder.build();            
// notification.flags =Notification.FLAG_ONGOING_EVENT;             
manager.notify(0, notification);

17)IntentFilter 是什麼?
簡述:結構化描述intent匹配的資訊。包含:action,categories and data(via type,scheme ,path),還有priority, to order multiple matching filters.


18)abortBroadcast() 函式的作用?

 

19)implements OnClickListener 和setOnClickListener 區別?即:Android中setOnClickListener和實現OnClickListener介面的區別?

setOnClickListener :一般是在同一個類裡面有很多按鈕或者控制元件需要新增OnClick事件,可以只定義一個OnClickListener,然後在裡面判斷是哪一個控制元件,然後做對應的操作;

public class MainActivity extends Activity implements OnClickListener {

 private Button sendNotice;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  sendNotice = (Button) findViewById(R.id.send_notice);
  sendNotice.setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
  switch (v.getId()) {
  case R.id.send_notice:
   
   .......
   break;
  default:
   break;
  }
 }

}

 

implements實現OnClickListener , OnClickListener 是 成員類,在Activity 建立的時候 它就要建立。
setOnClickListener ,你可以控制它 在必要的時候再 進行 建立。但是建立了之後,效果應該都是一樣的。
 



20)eclipse 如何匯入Android工程
file-->Import-->Android-->Existing Android Code Into Workspace


21)eclipse 專案不見了,如何顯示專案
使用eclipse的檢視,window -> show view -> package explorer


23)logcat中不斷的傳送以下訊息: Unexpected value from nativeGetEnabledTags: 0
API 17的虛擬機器會有這個問題


 As indicated above, we're aware of the issue. It's an issue in the emulator system-image for API 17. 
 We plan to distribute a new system-image for 17 that will resolve this, but I don't have any hard date to announce.
 Workarounds in between are to use the emulator with API 16 (doesn't help if you need the APIs for 17 of course) 
 or use a real device such as N4 or N7 or N10 that has API 17 on it.
 
 解決辦法:過濾掉日誌
 在logcat的過濾器的log message欄位中輸入以下過濾串。
 ^(?!.*(nativeGetEnabledTags)).*$  




 24)android OnResume函式在什麼時候呼叫;
 onResume是在啟動activity啟動之後才能執行的,也就是恢復執行。程式正常啟動:onCreate()->onStart()->onResume();
正常退出:onPause()->onStop()->onDestory()
一個Activity啟動另一個Activity: onPause()->onStop(), 再返回:onRestart()->onStart()->onResume()
程式按back 退出: onPause()->onStop()->onDestory(),再進入:onCreate()->onStart()->onResume();
程式按home 退出: onPause()->onStop(),再進入:onRestart()->onStart()->onResume(); 




25)OnSeekBarChangeListener 這個類的作用?
SeekBar拖動進度條


26)setOnTouchListener 中OnTouch什麼時候出發?Android onTouchEvent和setOnTouchListener中onTouch的區別?
區別如下:


1、如果setOnTouchListener中的onTouch方法返回值是true(事件被消費)時,則onTouchEvent方法將不會被執行;


2、只有當setOnTouchListener中的onTouch方法返回值是false(事件未被消費,向下傳遞)時,onTouchEvent方法才被執行。


3、以上說的情況適用於View物件(事件會最先被最內層的View物件先響應)而不是ViewGroup物件(事件會最先被最外層的View物件先響應)。


綜合來講:
onTouchListener的onTouch方法優先順序比onTouchEvent高,會先觸發。
假如onTouch方法返回false,會接著觸發onTouchEvent,反之onTouchEvent方法不會被呼叫。
內建諸如click事件的實現等等都基於onTouchEvent,假如onTouch返回true,這些事件將不會被觸發。


27)eclipse 如何顯示行號
window-->Preferences-->Editors-->Text Editors-->Show line numbers;




28)android 如何獲取藍芽其它裝置的uuid;獲取Android裝置的唯一識別碼|裝置號|序號|UUID 
http://www.cnblogs.com/xiaowenji/archive/2011/01/11/1933087.html




29)handler postdelayed 如何使用?
public final boolean postDelayed(Runnable r, long delayMillis)
延時delayMillis毫秒 將Runnable插入訊息列隊,
Runnable將在handle繫結的執行緒中執行。


30)java 回撥函式如何定義?




31)Activity runOnUiThread




32)BaseAdapter getView notifyDataSetChanged 呼叫順序?
http://www.cnblogs.com/kissazi2/p/3721941.html


getView是由framework呼叫的。如果要framework呼叫getView(),可以嘗試用BaseAdapter.notifyDataSetChanged ()方法試試。


32)android 藍芽如何只匹配某個裝置?怎麼實現android裡面的藍芽裝置的自動配對?
http://www.oschina.net/question/106603_54246


33)如何預先設定藍芽配對金鑰?




34)Avoid hardcoding the debug mode; leaving it out allows debug and release builds to automatically?
解決辦法:
http://sodino.com/2015/01/13/avoid-hardcoding-the-debug-mode/
Eclipse -> Preferences -> Android -> Lint Error Checking -> Issue -> Security -> HardCodeDebugMode -> Severity: -> Warning即可
或者刪掉android:debuggable="true"




35)android 支援串列埠通訊嗎?
android支援 多種方式的串列埠通訊,也提供了一個例子,見:
http://blog.sina.com.cn/s/blog_917f16620101fv3w.html
http://www.th7.cn/Program/Android/201409/281776.shtml
http://blog.csdn.net/ckw474404603/article/details/37811499


如何虛擬串列埠,通過手機模擬器進行測試;
http://m.blog.csdn.net/blog/sunjundelove/38399379


36)怎麼判斷android adb 有沒有裝好?




37)Installation error: INSTALL_FAILED_VERSION_DOWNGRADE
[2015-12-26 01:28:03 - myserial] Installation error: INSTALL_FAILED_VERSION_DOWNGRADE
[2015-12-26 01:28:03 - myserial] Please check logcat output for more details.
[2015-12-26 01:28:03 - myserial] Launch canceled!




將android:versionCode="32" 版本code 改大就可以了;


38)android 模擬器可以模擬一臺藍芽BLE裝置嗎?
不能,藍芽BLE需要真機測試;


39)android studio 工程如何轉換成eclipse工程?
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0110/2294.html
http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0918/1692.html


40)R cannot be resolved to a variable?
http://blog.sina.com.cn/s/blog_a8414f700101b4qr.html


41)adapter notifyDataSetChanged 作用?
因為notifyDataSetChanged()方法的確是通知adapter呼叫getview來重新整理每個Item;
http://wxwlulu.blog.163.com/blog/static/120384925201331734158806/


42)bindService 函式的作用?
     Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
     bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);


http://www.cnblogs.com/yejiurui/archive/2013/11/18/3429451.html
BindService和Started Service都是Service,有什麼地方不一樣呢:


1. Started Service中使用StartService()方法來進行方法的呼叫,呼叫者和服務之間沒有聯絡,即使呼叫者退出了;
服務依然在進行【onCreate()-  >onStartCommand()->startService()->onDestroy()】,注意其中沒有onStart(),主要是被onStartCommand()方法給取代了,onStart方法不推薦使用了。


2. BindService中使用bindService()方法來繫結服務,呼叫者和繫結者綁在一起,呼叫者一旦退出服務也就終止了【onCreate()->onBind()->onUnbind()->onDestroy()】。


43)android 如何做漂亮的app?
http://www.xueui.cn/tutorials/app-tutorials/design-android-ui.html


44)藍芽黑科技
http://drops.wooyun.org/tips/10109
藍芽4.2規範:
https://www.bluetooth.com/specifications/adopted-specifications

 

https://www.bluetooth.com/specifications/adopted-specifications
RFID 黑科技
http://www.evil0x.com/posts/5619.html

45)請教android如何做到service常駐記憶體?
android實現開機自啟動可能是移動作業系統中最簡單的了,我們只需要監聽一個開機啟動的Broadcast(廣播)即可。首先寫一個Receiver(即廣播監聽器),繼承BroadcastReceiver;

可以設定廣播註冊,
網路啟動時 自動啟動service
手機啟動時自動啟動service

1.如果是系統程式 可以配置android:persistent =true
2.非系統的可以在繫結的時候這樣
Notification notification = new Notification(); 
notification.flags = Notification.FLAG_ONGOING_EVENT; 
notification.flags |= Notification.FLAG_NO_CLEAR; 
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; 
service.startForeground(1, notification);  


46)Android--保持加速度感測器在螢幕關閉後執行;
http://www.cnblogs.com/taoweiji/p/3620329.html


47)BluetoothGattCallback 類的作用?
BluetoothGattCallback用於傳遞一些連線狀態及結果;
http://www.cnblogs.com/savagemorgan/p/3722657.html


48)Android 開發中 iBeacon的使用 .

http://blog.csdn.net/jie1991liu/article/details/47403455
http://stackoverflow.com/questions/18906988/what-is-the-ibeacon-bluetooth-profile

49)startDiscovery() and startLeScan() 關係?
You have to start a scan for Classic Bluetooth devices with startDiscovery() and a scan for Bluetooth LE devices with startLeScan(). Caution: Performing device

discovery is a heavy procedure for the Bluetooth adapter and will consume a lot of its resources.


50)Android BluetoothAdapter stopLeScan和startLeScan,傳進去的回撥函式必須是同一個?
http://blog.csdn.net/tim3366/article/details/43965313


51)ListView setTag 和setListAdapter setAdapter;


52)BluetoothDevice 類的connectGatt函式?
兩個裝置通過BLE通訊,首先需要建立GATT連線。這裡我們講的是Android裝置作為client端,連線GATT Server。
連線GATT Server,你需要呼叫BluetoothDevice的connectGatt()方法。此函式帶三個引數:Context、autoConnect(boolean)和BluetoothGattCallback物件。呼叫示例:
 
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
 
函式成功,返回BluetoothGatt物件,它是GATT profile的封裝。通過這個物件,我們就能進行GATT Client端的相關操作。BluetoothGattCallback用於傳遞一些連線狀態及結果。
 
BluetoothGatt常規用到的幾個操作示例:
 
connect() :連線遠端裝置。
discoverServices() : 搜尋連線裝置所支援的service。
disconnect():斷開與遠端裝置的GATT連線。
close():關閉GATT Client端。
readCharacteristic(characteristic) :讀取指定的characteristic。
setCharacteristicNotification(characteristic, enabled) :設定當指定characteristic值變化時,發出通知。
getServices() :獲取遠端裝置所支援的services。
 


Android中進行藍芽開發需要使用到的類的執行過程是:

1、使用BluetoothAdapter.startLeScan來掃描低功耗藍芽裝置

2、在掃描到裝置的回撥函式中會得到BluetoothDevice物件,並使用BluetoothAdapter.stopLeScan停止掃描

3、使用BluetoothDevice.connectGatt來獲取到BluetoothGatt物件;同時通過BluetoothDevice.connectGatt的函式的第3個引數獲取連線的回撥;在回撥函式中執行

BluetoothGatt.discoverServices;

4、執行BluetoothGatt.discoverServices,這個方法是非同步操作,在回撥函式onServicesDiscovered中得到status,通過判斷status是否等於BluetoothGatt.GATT_SUCCESS來判斷查詢

Service是否成功;

//以下api由android自己封裝好了;只要呼叫mBluetoothGatt.readCharacteristic(characteristic);或mBluetoothGatt.writeCharacteristic(characteristic); 就可以和藍芽ble裝置通訊

5、如果成功了,則通過BluetoothGatt.getService來獲取BluetoothGattService

6、接著通過BluetoothGattService.getCharacteristic獲取BluetoothGattCharacteristic

7、然後通過BluetoothGattCharacteristic.getDescriptor獲取BluetoothGattDescriptor


53)BluetoothGatt.discoverServices 函式的作用?
發現遠端的ble的service;從onServicesDiscovered 可以獲取結果回撥到UI上顯示;
http://developer.android.com/reference/android/bluetooth/BluetoothGatt.html

54)onCharacteristicRead 回撥怎麼執行的?
readCharacteristic 函式執行完後,非同步呼叫onCharacteristicRead 函式得到讀的結果;
http://stackoverflow.com/questions/26268713/android-ble-oncharacteristicread-appears-to-be-blocked-by-thread

55)onReadRemoteRssi 什麼時候呼叫?
http://developer.android.com/reference/android/bluetooth/BluetoothGattCallback.html
Class Overview
This abstract class is used to implement BluetoothGatt callbacks.

Summary
Public Constructors
BluetoothGattCallback()
Public Methods
void  onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
Callback triggered as a result of a remote characteristic notification.

void  onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
Callback reporting the result of a characteristic read operation.

void  onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
Callback indicating the result of a characteristic write operation.

void  onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
Callback indicating when GATT client has connected/disconnected to/from a remote GATT server.

void  onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status)
Callback reporting the result of a descriptor read operation.

void  onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status)
Callback indicating the result of a descriptor write operation.

void  onMtuChanged(BluetoothGatt gatt, int mtu, int status)
Callback indicating the MTU for a given device connection has changed.

void  onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status)
Callback reporting the RSSI for a remote device connection.

void  onReliableWriteCompleted(BluetoothGatt gatt, int status)
Callback invoked when a reliable write transaction has been completed.

void  onServicesDiscovered(BluetoothGatt gatt, int status)
Callback invoked when the list of remote services, characteristics and descriptors for the remote device have been updated, ie new services have been discovered.
[Expand]
Inherited Methods
 From class java.lang.Object


 

56)藍芽ble的配對問題
http://www.deyisupport.com/question_answer/wireless_connectivity/bluetooth/f/103/t/59481.aspx
http://blog.csdn.net/feilusia/article/details/50212945


57)藍芽BLE裝置如何知道 已經配對過,已經繫結過呢?藍芽BLE 裝置可以支援和多個手機繫結嗎?
通過手機儲存;


58)android  開源的ui
http://blog.csdn.net/mao520741111/article/details/45581881

 

59)adb ubuntu adb devices no permissions

sudo adb kill-server

sudo adb start-server

sudo adb devices

 

 

60)問題解決-Failed to resolve: com.android.support.constraint:constraint-layout:1.0.0-alpha7

https://www.cnblogs.com/zly1022/p/7771544.html

 

61)Minimum supported Gradle version is 4.1. Current version is 2.14.1.

https://blog.csdn.net/qq_36317441/article/details/78539035

https://blog.csdn.net/CHITTY1993/article/details/78667069

Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated

 

相關文章