Posts

Showing posts with the label android

获取百度地图sdk的sha1

最近做的东西涉及到使用百度地图sdk,发现在注册百度地图key的时候需要一个开发版sha1和一个发布版的sha1,这两个值估计是用来验证程序完整性的。 这个东西困扰了我一段时间,我也查了很多资料,但都没什么用。我在具体研究了之后,刚好摸出来了一个方法,现在来讲讲怎么获取这两个值: 首先是开发版sha1,这个比较简单。 先记录下自己debug.keystore文件的位置,我的电脑上这个文件在这个位置: C:\Users\用户名\.android\debug.keystore 。 然后找到自己jdk的bin文件夹的位置,我的是这个: C:\Program Files\Java\jdk-17.0.2\bin 。 现在打开终端,先将文件夹指向jdk的bin文件夹: cd C:\Program Files\Java\jdk-17.0.2\bin 。 然后输入这个命令: keytool -list -v -keystore C:\Users\用户名\.android\debug.keystore 注意:debug.keystore文件的位置需要自行调整, 密码为:android 到此获取开发版本sha1就完成了。 获取发布版的sha1跟上面的步骤差不多,只不过debug.keystore是自己打包app时用的签名文件(格式为jks的文件)。

Android 定位使用

最近给车写了个速度仪表程序,需要用到GPS,所以在这里记录一下Android中的GPS使用方式。 第一步要在 AndroidManifest.xml 里面加入这行:     <pre><code>&lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;</code></pre> 来申请访问位置的权限。 第二步,在MainActivity里定义并初始化 LocationManager 和 locationProvider。比如: <pre><code>LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);</code></pre> LocationManager的主要作用是初始化GPS服务,方便后面用GPS。 locationProvider是一个string变量,储存的定位方式(网络,基站,GPS)。初始化方法如下: String locationProvider = locationManager.GPS_PROVIDER; 我这里图方便直接用GPS,毕竟这个最准。 第三步,获取定位。这一步其实很简单,需要声明一个Location变量,然后让locationManager不停的请求新位置。 (这个必须要先申请权限,要不然没办法运行) 初始化Location变量,先获取一下最后已经知道的位置: <pre><code>Location location = locationManager.getLastKnownLocation(locationProvider);</code></pre> 再让locationManager不停请求新位置,用的是这个方法:requestLocationUpdates() 比如: <pre><code> locationManager.requestLocationUpdates(locationP...

Android ListView 简单使用

最近在做车子的速度仪表,有个环节要用到ListView,正好写写怎么用这个。ListView这个东西用自带的ArrayAdapter挺简单的,可惜没有什么自定义功能,不过这也够我用。第一步,在layout里声明一下ListView: <pre><code> &lt;ListView         android:id="@+id/lv"         android:layout_width="match_parent"         android:layout_height="match_parent"         /&gt;</code></pre> 第二步,声明并初始化ListView和ArrayAdapter的变量: <pre><code> Listview lv = findViewById(R.id.lv); ArrayAdapter av = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data); lv.setAdapter(av); </code></pre> 这里的这个data是指的填充ListView的数据。到这里就有一个能正常显示内容的ListView了。ListView每行显示的内容其实都是能点击的,想要实现点击功能很简单,加一个监听器就行: <pre><code> private AdapterView.OnItemClickListener onItemClickListener = (adapterView, view, i, l) -> {         //点击后的动作     }; </code></pre> 上面这段代码里比较有用的就是这个i变量,储存了用户点击的位置。注:这段代码是放在activity里的,声明监听器的方式也不只这一种。最后,把监听器和List...

Implementing Multi-Selection in Android ListView

This is a feature I've wanted to implement for a while but wasn't sure how. I finally figured it out these past few days, so I'm writing it down here to record it. To implement multi-selection for a ListView , you need to set a ChoiceMode and a multi-selection listener. ChoiceMode refers to how many selections the user can make on the ListView (single, multiple). The ChoiceMode for multi-selection is set like this: lv.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL); Note: To use AbsListView , you need to import android.widget.AbsListView beforehand. While changing the ListView, it's also best to change the layout in your array adapter to this, so the user knows what they've selected: android.R.layout.simple_list_item_multiple_choice Implementing the multi-selection listener is also very simple: rideListView.setMultiChoiceModeListener( new AbsListView.MultiChoiceModeListener() { @Override public void onItemCheckedStateChanged (ActionMode actionM...