2月10日 体温APP开发总结
1、Java代码
1、user
package bean;
public class User {
private String name;
private String riqi;
private String time;
private String space;
private String wen;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setRiqi(String riqi) {
this.riqi = riqi;
}
public String getRiqi() {
return riqi;
}
public void setTime(String time) {
this.time = time;
}
public String getTime() {
return time;
}
public void setSpace(String space) {
this.space = space;
}
public String getSpace() {
return space;
}
public void setWen(String wen) {
this.wen = wen;
}
public String getWen() {
return wen;
}
public User(String name,String riqi,String time,String space,String wen){
this.name=name;
this.riqi=riqi;
this.time=time;
this.space=space;
this.wen=wen;
}
}
2、dao
package dao;
import android.os.Build;
import androidx.annotation.RequiresApi;
import java.sql.*;
import bean.User;
import util.DBUtil;
public class Dao {
public void add(User user) {
//获得链接对象
Connection connection = DBUtil.getConn("users");
//准备sql语句
String sql = "insert into text(name,riqi,time,space,wen)values(?,?,?,?,?)";
try {
//创建语句传输对象
if(connection!=null) {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
if (preparedStatement != null) {
preparedStatement.setString(1, user.getName());
preparedStatement.setString(2, user.getRiqi());
preparedStatement.setString(3, user.getTime());
preparedStatement.setString(4, user.getSpace());
preparedStatement.setString(5, user.getWen());
preparedStatement.executeUpdate();
preparedStatement.close();
connection.close();
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3、DBUtil
package util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBUtil{
private static String driver = "com.mysql.jdbc.Driver";// MySql驱动
private static String user = "用户名";
private static String password = "密码";
public static Connection getConn(String dbname){
Connection connection = null;
try{
Class.forName(driver);// 动态加载类
String ip = "本机IP";
// 尝试建立到给定数据库URL的连接
connection = DriverManager.getConnection("jdbc:mysql://" + ip + ":3306/" + dbname, user, password);
}catch (Exception e){
e.printStackTrace();
}
return connection;
}
}
4、main_activity
package com.example.Temper;
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("\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 "获取失败!";
}
}
2、页面代码
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="10"
android:columnCount="4"
> <!--设置网格为10行4列-->
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnSpan = "1"
android:layout_marginLeft="4px"
android:gravity="left"
android:text="姓名:"
android:textSize="30dip" />
<EditText
android:id="@+id/et_name"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_columnSpan = "3"
android:layout_marginLeft="4px"
android:gravity="left"
android:text=""
android:textSize="30dip" />
<Button
android:id="@+id/bt_riqi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:background="#EE6363"
android:text="获取日期"
android:textColor="#FFF0F5"
android:textSize="26sp" />
<TextView
android:id="@+id/tv_riqi0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnSpan = "1"
android:layout_marginLeft="4px"
android:gravity="left"
android:text="日期:"
android:textSize="30dip" />
<TextView
android:id="@+id/tv_riqi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnSpan = "3"
android:layout_marginLeft="4px"
android:gravity="left"
android:text=""
android:textSize="30dip" />
<Button
android:id="@+id/bt_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:background="#EE6363"
android:text="获取时间"
android:textColor="#FFF0F5"
android:textSize="26sp" />
<TextView
android:id="@+id/tv_time0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnSpan = "1"
android:layout_marginLeft="4px"
android:gravity="left"
android:text="时间:"
android:textSize="30dip" />
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnSpan = "3"
android:layout_marginLeft="4px"
android:gravity="left"
android:text=""
android:textSize="30dip" />
<Button
android:id="@+id/bt_space"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EE6363"
android:layout_columnSpan="4"
android:textColor="#FFF0F5"
android:text="获取地点"
android:textSize="26sp" />
<TextView
android:id="@+id/tv_space0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnSpan = "1"
android:layout_marginLeft="4px"
android:gravity="left"
android:text="地点:"
android:textSize="30dip" />
<TextView
android:id="@+id/tv_space"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_columnSpan = "3"
android:layout_marginLeft="4px"
android:gravity="left"
android:textSize="30dip" />
<TextView
android:id="@+id/tv_wen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnSpan = "1"
android:layout_marginLeft="4px"
android:inputType="number"
android:gravity="left"
android:text="体温:"
android:textSize="30dip" />
<EditText
android:id="@+id/et_wen"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_columnSpan = "3"
android:layout_marginLeft="4px"
android:gravity="left"
android:text=""
android:textSize="30dip" />
<Button
android:id="@+id/bt_insert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EE6363"
android:layout_columnSpan="4"
android:textColor="#FFF0F5"
android:text="提交"
android:textSize="26sp" />
<TextView
android:id="@+id/tv_tishi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnSpan = "4"
android:layout_marginLeft="4px"
android:gravity="left"
android:text=""
android:textSize="30dip" />
</GridLayout>
2月10日 体温APP开发总结的更多相关文章
- 2月4日 体温APP开发记录
1.阅读构建之法 现代软件工程(第三版) 2.观看Android开发视频教程最新版 Android Studio开发 3.数据库链接,,数据传输功能测试
- 2月3日 体温APP开发记录
1.阅读构建之法 现代软件工程(第三版) 2.观看Android开发视频教程最新版 Android Studio开发 3.回返地址学习,下载导入相关jar包
- 2月2日 体温APP开发记录
1.阅读构建之法 现代软件工程(第三版) 2.观看Android开发视频教程最新版 Android Studio开发 3.Edit text使用学习
- 2月1日 体温APP开发记录
1.阅读构建之法 现代软件工程(第三版) 2.观看Android开发视频教程最新版 Android Studio开发
- 1月29日 体温APP开发记录
1.阅读构建之法 现代软件工程(第三版) 2.观看Android开发视频教程最新版 Android Studio开发 3.高德地图API下载获取key
- 转载:爱加密邀您参加5月17日深圳App开发沙龙活动
听闻,移动互联网的九大门派已集结在一起,各路顶尖高手携App修炼宝典九阴真经现身,一场席卷全国的App修炼风暴即将来临.5月17日,以“移动开发者如何白手起家.快速盈利”为主题的沙龙活动将在深圳市南山 ...
- 【2017年9月10日更新】ABP配套代码生成器(ABP Code Generator)帮助文档,实现快速开发
ABP代码生成器介绍 ABP Code Generator 针对abp这个框架做了一个代码生成器,功能强大.分为两大功能点,一个是数据层,一个是视图层. 数据服务层:通过它,可以实现表设计.领域层初始 ...
- 成都Uber优步司机奖励政策(4月10日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 北京Uber优步司机奖励政策(4月10日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
随机推荐
- Birt报表设置自定义的值
比如数据库查出该字段的值有"no",有"yes",那么想要根据当是no是显示"未完成",当是yes时显示"已完成" 可以 ...
- AcWing429. 奖学金
题目: 某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前5名学生发奖学金. 期末,每个学生都有3门课的成绩:语文.数学.英语. 先按总分从高到低排序,如果两个同学总分相同,再按语文成绩从 ...
- 【LeetCode】1064. Fixed Point 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 日期 题目地址:https://leetco ...
- 【LeetCode】118. Pascal's Triangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 【LeetCode】926. Flip String to Monotone Increasing 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Prefix计算 动态规划 参考资料 日期 题目地址 ...
- 【LeetCode】677. Map Sum Pairs 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 前缀树 日期 题目地址:https://lee ...
- 【LeetCode】142. Linked List Cycle II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 set 日期 题目地址:https://le ...
- Visible Trees(hdu2841)
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- ZOJ 3785:What day is that day?(数论)
What day is that day? Time Limit: 2 Seconds Memory Limit: 65536 KB It's Saturday today, what day is ...
- oralce索引的使用
1.索引的作用 数据库对象 用于提高数据库检索的效率,对于where,group,order by条件中经常出现的字段,创建索引可以加快效率 缺点:如果对于大量的数据插入时效率可能会变低 2.索引的使 ...