android遇到的问题记录

1 用gson对json进行解析时候非空字段的判断:

gson = new GsonBuilder().serializeNulls().create();

2 让linearlayout下拉到顶部时候可继续下拉出空白区域然后回弹至顶部的方法:

继承一个scrollview,然后重写ontouchevent方法如下:

@Override
public boolean onTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub

switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
if (view != null)
mNormalRect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
break;
case MotionEvent.ACTION_MOVE:
int temp = (int) ((ev.getY() - m_y) * 0.25);
if(temp>0&&mNormalRect!=null)
view.layout(view.getLeft(), view.getTop() + temp, view.getRight(), view.getBottom() + temp);
break;
case MotionEvent.ACTION_UP:
if(mNormalRect!=null){
view.layout(mNormalRect.left, mNormalRect.top, mNormalRect.right, mNormalRect.bottom);
TranslateAnimation animation = new TranslateAnimation(0, 0, view.getTop() - mNormalRect.top, 0);
animation.setDuration(200);
startAnimation(animation);
}
break;
}
m_y = ev.getY();
return super.onTouchEvent(ev);
}

3:单选dialog的风格设置如下:

<style name="CustomDialog" parent="@android:style/Theme.Holo.Light.Dialog">  
        <!--背景颜色及透明程度-->  
        <item name="android:windowBackground">@android:color/transparent</item>  
        <!--是否有标题 -->  
        <item name="android:windowNoTitle">true</item>  
        <!--是否浮现在activity之上-->  
        <item name="android:windowIsFloating">true</item>  
        <!--是否模糊-->  
        <item name="android:backgroundDimEnabled">true</item>  
    </style>  


4:蓝牙实现自动配对:(暂时只能实现自动输入配对码进行配对,无法实现配对对话框不显示的问题)

1:首先查看蓝牙是否打开,没有发开进行打开

2:打开蓝牙之后注册蓝牙监听广播

BluetoothAdapter.ACTION_DISCOVERY_STARTED //开始搜索设备

BluetoothDevice.ACTION_FOUND //搜索设备中

BluetoothAdapter.ACTION_DISCOVERY_FINISHED //搜索设备结束

BluetoothDevice.ACTION_BOND_STATE_CHANGED//绑定状态发生改变

android.bluetooth.device.action.PAIRING_REQUEST //自动配对时候收到的action

3::触发蓝牙进行配对

ClsUtils.createBond(btDevice.getClass(), btDevice);

其中createBond方法如下:

static public boolean createBond(Class btClass, BluetoothDevice btDevice) throws Exception {
Method createBondMethod = btClass.getMethod("createBond");
Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
return returnValue.booleanValue();
}

采用反射机制调用createBondMethod;

然后在receiver中对action进行判断,如果是android.bluetooth.device.action.PAIRING_REQUEST这action,则进行如下操作:

setPin(device.getClass(), device, strPsw);

createBond(device.getClass(), device);

//cancelPairingUserInput(device.getClass(), device);此方法也是调用java的反射机制获取到源码中的取消对话框弹出的方法,但是测试了没有效果,如果有知道的朋友可以发表评论,让大家都学习学习;

最后进行页面设计,完成蓝牙配对工作。






おすすめ

転載: blog.csdn.net/daxiangzaici/article/details/52134208