今天坐成了体温app。
mainactivity代码:
package com.example.tiwen;
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.core.app.ActivityCompat;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import bean.User;
import dao.Dao;
public class MainActivity extends Activity implements View.OnClickListener {
Button bt_riqi,bt_time,bt_space,bt_insert;
EditText et_name,et_wen;
TextView tv_riqi,tv_riqi0,tv_time,tv_space,tv_tishi;
private static final String[] authBaseArr = {//申请类型
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
};
private static final int authBaseRequestCode = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_riqi=(Button)findViewById(R.id.bt_riqi);
bt_time=(Button)findViewById(R.id.bt_time);
bt_space=(Button)findViewById(R.id.bt_space);
bt_insert=(Button)findViewById(R.id.bt_insert);
et_name=(EditText) findViewById(R.id.et_name);
tv_riqi=(TextView)findViewById(R.id.tv_riqi);
tv_riqi0=(TextView)findViewById(R.id.tv_riqi0);
tv_time=(TextView)findViewById(R.id.tv_time);
tv_space=(TextView)findViewById(R.id.tv_space);
et_wen=(EditText) findViewById(R.id.et_wen);
tv_tishi=(TextView)findViewById(R.id.tv_tishi);
bt_riqi.setOnClickListener(this);
bt_time.setOnClickListener(this);
bt_space.setOnClickListener(this);
bt_insert.setOnClickListener(this);
}
@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(" 纬度:");
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 "获取失败!";
}
}