android 开发中常见问题

来源:互联网 发布:手机html编程工具 编辑:程序博客网 时间:2024/05/16 02:59

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

0 0