android 常用小知识点 tips(一)
16lz
2021-12-04
本人总结的一些android的小知识点,会一直持续更新的,忘关注!
PS:由于上不支持markdown 的[TOC]语法,所以没法显示目录,
android 常用小知识点 tips (一)
android 常用小知识点 tips (二)
[TOC]
1、android 6.0 权限申请
/** * android 6.0 申请权限 */ private void requestLocalPermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED ) { //申请WRITE_EXTERNAL_STORAGE权限 ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE, }, 0); } } /** * 申请权限完成后回调 */ @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); }
2、URLDecoder
URLDecoder.decode(value, "utf-8");URLEncoder.encode(value, "utf-8"); -----------------------------------java端另一种解决方法--------------------------------------------jsp页面上有一个文本框:当文本框内容是汉字或者日文的时候,servlet中获得此文本框内容时是乱码:request.getParameter("companyName");解决:String str = request.getParameter("companyName");当文本框是中文时:new String(str.getBytes("ISO-8859-1"), "GB2312");当文本框是日文时:new String(str.getBytes("ISO8859-1"), "UTF-8");
3、Url 解析
// https://www.baidu.com?action=jump&target_page=ZZ1103¤t_page=ZZ11000; private void dispatchUriTest(Uri uri) { try { String domain = uri.getAuthority(); String buffer = uri.getQueryParameter("action"); String page = uri.getQueryParameter("target_page"); String cur_page = uri.getQueryParameter("current_page"); Toast.makeText(this, type + " " + buffer, Toast.LENGTH_SHORT).show(); } catch (Exception e) { Log.e(TAG, "Uri Parse Error"); } }PS:url地址中不能有#号,如有的话,请主动替换掉。
4、activity 和 fragment 里面数据备份
//fragment @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("requestDate", requestDate); outState.putParcelableArrayList("dates", dates); } @Override public void onViewStateRestored(@Nullable Bundle savedInstanceState) { super.onViewStateRestored(savedInstanceState); if(savedInstanceState!=null) { requestDate = savedInstanceState.getString("requestDate"); dates = savedInstanceState.getParcelableArrayList("dates"); } } //Activity @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("matchId", matchId); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); if (savedInstanceState != null) { matchId = savedInstanceState.getString("matchId"); } }
5、gradle 打包命令
gradlew assembleReleaseA8FlavorReleaseA8gradlew assemble+ flavor + buildType 的格式组成的
6、android 蓝牙开发
Android 蓝牙编程的基本步骤: 获取蓝牙适配器BluetoothAdapter blueadapter=BluetoothAdapter.getDefaultAdapter(); 如果BluetoothAdapter 为null,说明android手机没有蓝牙模块。判断蓝牙模块是否开启,blueadapter.isEnabled() true表示已经开启,false表示蓝牙并没启用。启动配置蓝牙可见模式,即进入可配对模式Intent in=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200); startActivity(in); ,200就表示200秒。获取蓝牙适配器中已经配对的设备Set device=blueadapter.getBondedDevices(); 还需要在androidManifest.xml中声明蓝牙的权限 接下来就是根据自己的需求对BluetoothAdapter 的操作了。
7、gps 获取经纬度
/************************* GPS定位 相关 end *****************************/ /** * android 6.0 申请权限 */ private void requestLocalPermission() { if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) { //申请WRITE_EXTERNAL_STORAGE权限 ActivityCompat.requestPermissions(getActivity(), new String[]{ Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, }, 0); } } /** * 申请权限完成后回调 */ @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); getLocation(context); } private LocationManager locationManager; private String locationProvider; //位置提供器 private String locationStr = ""; private void getLocation(Context context) { //1.获取位置管理器 locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); //2.获取位置提供器,GPS或是NetWork List providers = locationManager.getProviders(true); if (providers.contains(LocationManager.NETWORK_PROVIDER)) { //如果是网络定位 locationProvider = LocationManager.NETWORK_PROVIDER; } else if (providers.contains(LocationManager.GPS_PROVIDER)) { //如果是GPS定位 locationProvider = LocationManager.GPS_PROVIDER; } else { Toast.makeText(context, "没有可用的位置提供器", Toast.LENGTH_SHORT).show(); return; } //3.获取上次的位置,一般第一次运行,此值为null if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } Location location = locationManager.getLastKnownLocation(locationProvider); if (location!=null){ showLocation(location); }else{ // 监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace locationManager.requestLocationUpdates(locationProvider, 0, 0,mListener); } } private void showLocation(Location location){ locationStr = location.getLatitude()+","+location.getLongitude(); Log.i(TAG, "gps : " + locationStr); } LocationListener mListener = new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } // 如果位置发生变化,重新显示 @Override public void onLocationChanged(Location location) { showLocation(location); } }; /************************* GPS定位 相关 end *****************************/
8、ibeacon 蓝牙设备
/************************* 蓝牙ibeacon 相关 begin *****************************/ private BluetoothAdapter mBluetoothAdapter; private void initBlueTooth() { mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); scanBLE(); openRemoveTimeOutDevicesTimer(); } @SuppressWarnings("deprecation") @SuppressLint("NewApi") public void scanBLE() { mBluetoothAdapter.startLeScan(mLeScanCallback); } @SuppressLint("NewApi") private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { final iBeaconClass.iBeacon ibeacon = iBeaconClass.fromScanData(device, rssi, scanRecord); addDevicesToMap(ibeacon); } }; Timer mRemoveTimeOutDevicesTimer; private void openRemoveTimeOutDevicesTimer() { mRemoveTimeOutDevicesTimer = new Timer(); mRemoveTimeOutDevicesTimer.schedule(new TimerTask() { @Override public void run() { if (mBleDeviceMaps.size() == 0) { scanBLE(); } } }, 1000, 1000); } private void stopTimer() { mRemoveTimeOutDevicesTimer.cancel(); } private ConcurrentHashMap mBleDeviceMaps = new ConcurrentHashMap(); // private ConcurrentHashMap mBlePutTime = new ConcurrentHashMap(); private ConcurrentHashMap mBleRssi = new ConcurrentHashMap(); private void addDevicesToMap(iBeaconClass.iBeacon device) { if (device == null) { Log.d("TAG", "device==null "); return; } // 只搜索与我们自己布置的iBeacon // TODO 章鱼彩票测试的时候,可以先把这段代码去掉 if (device.getProximityUuid() != null) { if (!(device.getProximityUuid().equals(ConstantUtil.HH_IBEACON_UUID))) { return; } } else { return; } // 键值对存起来 // 接收到一个信号,如果没有,就存入 // 通过address比较 // 如果已有就更新 // 如果长时间不更新,就删除 // 用另一个map记录时间 // 如果3秒没有信号 // 删除两个map中的数据 mBleDeviceMaps.put(device.bluetoothAddress, device);// mBlePutTime.put(device.bluetoothAddress, System.currentTimeMillis()); mBleRssi.put(device.bluetoothAddress, device.rssi); } //---------------------------工具函数------------------------------------ public String[] sortMapvalues(ConcurrentHashMap map) { // 排序hashmap的值 List> retArray = null; retArray = new ArrayList>(map.entrySet()); Collections.sort(retArray, new Comparator>() { public int compare(Map.Entry o1, Map.Entry o2) { if (o2.getValue() != null && o1.getValue() != null && o2.getValue().compareTo(o1.getValue()) > 0) { return 1; } else { return -1; } } }); // 按顺序取出另个集合中的iBeacon列表 String ret[] = new String[retArray.size()]; for (int i = 0; i < retArray.size(); i++) { iBeaconClass.iBeacon iB = mBleDeviceMaps.get(retArray.get(i).getKey()); if (iB != null) { ret[i] = iB.getBluetoothAddress(); } } return ret; } private JSONObject makeJsonParam() { String retJson = null; String[] objects = sortMapvalues(mBleRssi); // 按信号远近进行排序 JSONObject ibeacon = new JSONObject(); JSONArray iBeaconList = new JSONArray(); for (int i = 0; i < objects.length; i++) { // 最多四条 if (i > 3) { continue; } // 生成每条内容 try { iBeaconClass.iBeacon ibe = mBleDeviceMaps.get(objects[i]); ibeacon.put("name", ibe.name); ibeacon.put("uuid", ibe.proximityUuid); ibeacon.put("address", ibe.bluetoothAddress); ibeacon.put("major", ibe.major); ibeacon.put("minor", ibe.minor); ibeacon.put("txPower", ibe.txPower); ibeacon.put("rssi", ibe.rssi); } catch (JSONException e3) { e3.printStackTrace(); } iBeaconList.put(ibeacon); ibeacon = new JSONObject(); } JSONObject paramObj = new JSONObject(); try { JSONObject location = new JSONObject(); location.put("iBeacons", iBeaconList); location.put("gps", locationStr); paramObj.put("locationInfo", location); paramObj.put("clientInfo", JsonUtil.getInstance().converData2String(getFilledClientInfoWrapper(context))); } catch (JSONException e1) { e1.printStackTrace(); } return paramObj; } /*** * 填充公共参数clientInfo * * @param context * @return */ protected RequestClientInfoWrapper getFilledClientInfoWrapper(Context context) { RequestClientInfoWrapper wrapper = new RequestClientInfoWrapper(); RequestClientInfoWrapper.fillInfo(context, wrapper); return wrapper; } /************************* 蓝牙ibeacon 相关 end *****************************/
9、获取android签名文件的信息
以管理员方式打开cd到对应目录,输入命令:keytool -list -v -keystore debug.keystore
10、android:windowSoftInputMode属性
熟悉利用配置文件,可以减少很多不必要的麻烦,下面附上android:windowSoftInputMode属性:stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activity里的状态,无论是隐藏还是显示stateHidden:用户选择activity时,软键盘总是被隐藏stateAlwaysHidden:当该Activity主窗口获取焦点时,软键盘也总是被隐藏的stateVisible:软键盘通常是可见的stateAlwaysVisible:用户选择activity时,软键盘总是显示的状态adjustUnspecified:默认设置,通常由系统自行决定是隐藏还是显示adjustResize:该Activity总是调整屏幕的大小以便留出软键盘的空间adjustPan:当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分
更多相关文章
- Android(安卓)软键盘在有scollview,纵向viewpager+recyclview实现
- Android软键盘弹出,界面整体上移的问题
- android 获取路径目录方法以及判断目录是否存在,创建目录
- Android(安卓)获取标题栏的高度
- 获取Android的Java源代码并在Eclipse中关联查看
- Android(安卓)EditText光标颜色 与inputType
- 获取Android的Java源代码并在Eclipse中关联查看
- Android开发实战-项目学习笔记(1)
- 〖Android〗简单隐藏Android虚拟键盘的方法