每日日报2021.2.12
Posted 1905-1雷宇
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每日日报2021.2.12相关的知识,希望对你有一定的参考价值。
今天完成内容:
开发体温app
1.编写代码
@Override
public void onClick(View v) {
String name=et_name.getText().toString();
String riqi="",time="",space="";
String wen=et_wen.getText().toString();
Date date = new Date();
SimpleDateFormat simpleDateFormat;
switch (v.getId()){
case R.id.bt_riqi:
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
riqi= simpleDateFormat.format(date);
tv_riqi.setText(riqi);
break;
case R.id.bt_time:
simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
time = simpleDateFormat.format(date);
tv_time.setText(time);
break;
case R.id.bt_space:
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
initNavi();
//权限检查的代码
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
return;
}
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,//指定GPS定位提供者
1000,//指定数据更新的间隔时间
1,//位置间隔的距离为1m
new LocationListener() {//监听GPS信息是否改变
@Override
public void onLocationChanged(Location location) {//GPS信息发送改变时回调
Log.i("lgq","onLocationChanged===="+location.getProvider());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {//GPS状态发送改变时回调
}
@Override
public void onProviderEnabled(String provider) { //定位提供者启动时回调
}
@Override
public void onProviderDisabled(String provider) { //定位提供者关闭时回调
}
}
);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);//获取最新的定位信息
if (location==null){
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,//指定GPS定位提供者
5000,//指定数据更新的间隔时间
10,//位置间隔的距离为1m
new LocationListener() {//监听GPS信息是否改变
@Override
public void onLocationChanged(Location location) {//GPS信息发送改变时回调
Log.i("lgq","onLocationChanged===="+location.getProvider());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {//GPS状态发送改变时回调
}
@Override
public void onProviderEnabled(String provider) { //定位提供者启动时回调
}
@Override
public void onProviderDisabled(String provider) { //定位提供者关闭时回调
}
}
);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);//获取最新的定位信息
}
locationUpdates(location);
break;
case R.id.bt_insert:
riqi=tv_riqi.getText().toString();
time=tv_time.getText().toString();
space=tv_space.getText().toString();
if(name.equals("")||name==""){
tv_tishi.setText("姓名为空,请输入姓名!");
}
else if(riqi.equals("")||riqi==""){
tv_tishi.setText("日期为空,请获取日期!");
}
else if(time.equals("")||time==""){
tv_tishi.setText("时间为空,请获取时间!");
}
else if(space.equals("")||space==""){
tv_tishi.setText("地点为空,请获取地点!");
}
else if(space.equals("GPS失效啦!")||space.equals("获取失败!")){
tv_tishi.setText("地点获取失败,请重新获取地点!");
}
else if(wen.equals("")||wen==""){
tv_tishi.setText("体温为空,请输入体温!");
}
else {
Dao dao = new Dao();
User user = new User(et_name.getText().toString(), tv_riqi.getText().toString(), tv_time.getText().toString(), tv_space.getText().toString(), et_wen.getText().toString());
new Thread(new Runnable() {
@Override
public void run() {
dao.add(user);
}
}).start();
tv_tishi.setText("提交成功!");
}
break;
}
}
private boolean hasBasePhoneAuth() {
PackageManager pm = getPackageManager();
for (String auth : authBaseArr) {
if (pm.checkPermission(auth, getPackageName()) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
private void initNavi() {
// 申请权限
if (android.os.Build.VERSION.SDK_INT >= 23) {
if (!hasBasePhoneAuth()) {
this.requestPermissions(authBaseArr, authBaseRequestCode);
return;
}
}
}
public void locationUpdates(Location location){
if(location != null){
StringBuilder stringBuilder = new StringBuilder(); //构建一个字符串构建器,用于记录定位信息
stringBuilder.append("经度:");
stringBuilder.append(location.getLongitude());
stringBuilder.append("\\n纬度:");
stringBuilder.append(location.getLatitude());
String ab = getAddress(location.getLatitude(),location.getLongitude());
tv_space.setText(ab);
}
else{
tv_space.setText("GPS失效啦!");
}
}
public String getAddress(double latitude, double longitude) {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
Address address = (Address) ((List) addresses).get(0);
String data = address.toString();
int startSpace = data.indexOf("\\"") + ":".length();
int endSpace = data.indexOf("\\"", startSpace);
String space=data.substring(startSpace,endSpace);
return space;
}
} catch (IOException e) {
e.printStackTrace();
}
return "获取失败!";
}
}
遇到问题:
无
明日目标:
学习Android studio的开发
以上是关于每日日报2021.2.12的主要内容,如果未能解决你的问题,请参考以下文章