这次寒假学校发布了一个制作安卓系统上上报体温app的任务,在这之前我根本没有接触过安卓软件的制作所以根本不知道该怎么做,好在最后根据网上的资料和自己的一些努力把app制作出来了,在这期间遇到到了很多困难。本次主要是对自己在软件开发进行总结。
无论干什么事情首先都要把工具准备好,安卓app开发的工具有eclipse和Android studio等,我一开始准备使用eclipse进行开发并且下载好了所需要的配件,后来发现eclipse开发起来有点麻烦所以最后重新下载了Android studio。这里贴一下我看的教程:https://blog.csdn.net/qq_41976613/article/details/91432304
环境配置好后我们就开始开发app,由于本次开发的app需要用到百度定位系统,因此我们还需要申请一个百度api,在获取百度api时我们需要有开发版和发布版SHA1,而获取发布版SHA1我们需要对自己的软件进行签名。
应用程序签名:https://blog.csdn.net/fukaimei/article/details/78294651?utm_source=app&app_version=4.5.1
百度api获取:https://blog.csdn.net/fukaimei/article/details/78736356?utm_source=app&app_version=4.5.1
在获取发布版SHA1可能会出现cd 无法转到其他盘的情况

此时我们可以在cd后加入/d来进行转换

除此外可能出现的问题:’keytool’ 不是内部或外部命令,也不是可运行的程序 或批处理文件
我们需要先找到keytool文件

然后打开命令窗口,先转到你的keytool文件夹路径下,输入keytool.exe,然后在后边输入命令 -list -keystore 输上后后边跟的就是你的debug.keystore或签名文件的对象路径 。

完成这些后我们就要开始正戏
Android studio自带数据库所以我们不需要下载数据库软件
表的创建方式

其余的增删改查的功能就按Java的方式写
最重要的定位功能
package com.example.mytem.Location;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.Poi;
import com.example.mytem.MainActivity;
import com.example.mytem.R;
import com.example.mytem.Location.locationservice;
public class locationActivity extends MainActivity {
private locationservice locationService;
private TextView mTextView;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
mTextView = findViewById(R.id.textview);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (button.getText().toString().equals("添加")) {
locationService.start();
} else {
locationService.stop();
button.setText("自动定位");
mTextView.setText("");
}
}
});
locationService = new locationservice(this);
// 多个activity
// locationService = ((App) getApplication()).locationService;
locationService.registerListener(mListener);
locationService.setLocationOption(locationService.getDefaultLocationClientOption());
}
@Override
protected void onDestroy() {
super.onDestroy();
locationService.unregisterListener(mListener); //注销掉监听
locationService.stop(); //停止定位服务
}
/*****
*
* 定位结果回调,重写onReceiveLocation方法,可以直接拷贝如下代码到自己工程中修改
*
*/
private BDAbstractLocationListener mListener = new BDAbstractLocationListener() {
@Override
public void onReceiveLocation(BDLocation location) {
// TODO Auto-generated method stub
if (null != location && location.getLocType() != BDLocation.TypeServerError) {
//button.setText("停止定位");
StringBuilder sb = new StringBuilder(256);
// sb.append("time : ");
/**
* 时间也可以使用systemClock.elapsedRealtime()方法 获取的是自从开机以来,每次回调的时间;
* location.getTime() 是指服务端出本次结果的时间,如果位置不发生变化,则时间不变
*/
// sb.append(location.getTime());
sb.append("\nlocType : ");// 定位类型
sb.append(location.getLocType());
sb.append("\nlocType description : ");// *****对应的定位类型说明*****
sb.append(location.getLocTypeDescription());
// sb.append("\nlatitude : ");// 纬度
// sb.append(location.getLatitude());
// sb.append("\nlontitude : ");// 经度
// sb.append(location.getLongitude());
// sb.append("\nradius : ");// 半径
// sb.append(location.getRadius());
// sb.append("\nCountryCode : ");// 国家码
// sb.append(location.getCountryCode());
// sb.append("\nCountry : ");// 国家名称
// sb.append(location.getCountry());
// sb.append("\ncitycode : ");// 城市编码
// sb.append(location.getCityCode());
// sb.append("\ncity : ");// 城市
// sb.append(location.getCity());
// sb.append("\nDistrict : ");// 区
// sb.append(location.getDistrict());
// sb.append("\nStreet : ");// 街道
sb.append(location.getStreet());
sb.append("\naddr : ");// 地址信息
sb.append(location.getAddrStr());
// sb.append("\nUserIndoorState: ");// *****返回用户室内外判断结果*****
// sb.append(location.getUserIndoorState());
// sb.append("\nDirection(not all devices have value): ");
// sb.append(location.getDirection());// 方向
sb.append("\nlocationdescribe: ");
sb.append(location.getLocationDescribe());// 位置语义化信息
// sb.append("\nPoi: ");// POI信息
// if (location.getPoiList() != null && !location.getPoiList().isEmpty()) {
// for (int i = 0; i < location.getPoiList().size(); i++) {
// Poi poi = (Poi) location.getPoiList().get(i);
// sb.append(poi.getName() + ";");
// }
// }
if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果
sb.append("\nspeed : ");
sb.append(location.getSpeed());// 速度 单位:km/h
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());// 卫星数目
sb.append("\nheight : ");
sb.append(location.getAltitude());// 海拔高度 单位:米
sb.append("\ngps status : ");
sb.append(location.getGpsAccuracyStatus());// *****gps质量判断*****
sb.append("\ndescribe : ");
sb.append("gps定位成功");
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果
// 运营商信息
if (location.hasAltitude()) {// *****如果有海拔高度*****
sb.append("\nheight : ");
sb.append(location.getAltitude());// 单位:米
}
sb.append("\noperationers : ");// 运营商信息
sb.append(location.getOperators());
sb.append("\ndescribe : ");
sb.append("网络定位成功");
} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
sb.append("\ndescribe : ");
sb.append("离线定位成功,离线定位结果也是有效的");
} else if (location.getLocType() == BDLocation.TypeServerError) {
sb.append("\ndescribe : ");
sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");
} else if (location.getLocType() == BDLocation.TypeNetWorkException) {
sb.append("\ndescribe : ");
sb.append("网络不同导致定位失败,请检查网络是否通畅");
} else if (location.getLocType() == BDLocation.TypeCriteriaException) {
sb.append("\ndescribe : ");
sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
}
logMsg(sb.toString());
}
}
};
/**
* 显示请求字符串
*/
public void logMsg(final String str) {
try {
new Thread(new Runnable() {
@Override
public void run() {
mTextView.post(new Runnable() {
@Override
public void run() {
mTextView.setText(str);
}
});
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.example.mytem.Location;
import android.content.Context;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
public class locationservice {
private LocationClient client = null;
private LocationClientOption mOption,DIYoption;
private Object objLock = new Object();
public locationservice(Context locationContext){
synchronized (objLock) {
if(client == null){
client = new LocationClient(locationContext);
client.setLocOption(getDefaultLocationClientOption());
}
}
}
// 注册
public boolean registerListener(BDAbstractLocationListener listener){
boolean isSuccess = false;
if(listener != null){
client.registerLocationListener(listener);
isSuccess = true;
}
return isSuccess;
}
// 注销
public void unregisterListener(BDAbstractLocationListener listener){
if(listener != null){
client.unRegisterLocationListener(listener);
}
}
//设置配置
public boolean setLocationOption(LocationClientOption option){
boolean isSuccess = false;
if(option != null){
if(client.isStarted())
client.stop();
DIYoption = option;
client.setLocOption(option);
}
return isSuccess;
}
//默认Option设置
public LocationClientOption getDefaultLocationClientOption(){
if(mOption == null){
mOption = new LocationClientOption();
mOption.setLocationMode(LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
mOption.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系,如果配合百度地图使用,建议设置为bd09ll;
mOption.setScanSpan(3000);//可选,默认0,即仅定位一次,设置发起连续定位请求的间隔需要大于等于1000ms才是有效的
mOption.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
mOption.setIsNeedLocationDescribe(true);//可选,设置是否需要地址描述
mOption.setNeedDeviceDirect(false);//可选,设置是否需要设备方向结果
mOption.setLocationNotify(false);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
mOption.setIgnoreKillProcess(true);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
mOption.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
mOption.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
mOption.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集
mOption.setOpenGps(true);//可选,默认false,设置是否开启Gps定位
mOption.setIsNeedAltitude(false);//可选,默认false,设置定位时是否需要海拔信息,默认不需要,除基础定位版本都可用
}
return mOption;
}
//自定义Option设置
public LocationClientOption getOption(){
if(DIYoption == null) {
DIYoption = new LocationClientOption();
}
return DIYoption;
}
public void start(){
synchronized (objLock) {
if(client != null && !client.isStarted()){
client.start();
}
}
}
public void stop(){
synchronized (objLock) {
if(client != null && client.isStarted()){
client.stop();
}
}
}
public boolean isStart() {
return client.isStarted();
}
public boolean requestHotSpotState(){
return client.requestHotSpotState();
}
}
在编程之中我还出现了这样的问题

给我的提示是cannot resolve symbol ‘recyclerview‘
最后发现是没有添加‘recyclerview‘依赖库的文件
方法:打开file-project structure

搜索

最后成果

定位失败一般是因为手机没给权限。
原文:https://www.cnblogs.com/Akko/p/14461993.html