首先是APP界面
上交后数据库显示为
然后是源码
MainActivity.java
package com.example.temp;import androID.Manifest;import androID.content.ContentValues;import androID.content.DialogInterface;import androID.content.Intent;import androID.content.pm.PackageManager;import androID.database.sqlite.sqliteDatabase;import androID.net.Uri;import androID.os.Bundle;import androID.provIDer.Settings;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.EditText;import androID.Widget.TextVIEw;import androIDx.appcompat.app.AlertDialog;import androIDx.appcompat.app.AppCompatActivity;import androIDx.core.app.ActivityCompat;import androIDx.core.content.ContextCompat;import com.baIDu.location.*;import java.text.SimpleDateFormat;import java.util.*;public class MainActivity extends AppCompatActivity { DBHelper dbHelper=null; public LocationClIEnt mLocationClIEnt = null; private MyLocationListener myListener = new MyLocationListener(); private final int mRequestCode = 100; List<String> mPermissionList = new ArrayList<>(); //声明一个数组,将需要申请的权限放在里面, 可以是一个权限,也可以是多个权限(你需要什么权限就添加什么权限) String[] permissions = new String[]{ Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET, Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.CHANGE_WIFI_STATE, }; public String getDate() { SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm"); Date date = new Date(System.currentTimeMillis()); return sd.format(date); } @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); TextVIEw text_time = findVIEwByID(R.ID.text_time); text_time.setText("\n\n" + getDate()); dbHelper=new DBHelper(getApplicationContext()); //权限 initPermission(); } public voID update() { TextVIEw text_time = findVIEwByID(R.ID.text_time); TextVIEw text_name = findVIEwByID(R.ID.text_name); TextVIEw text_tem = findVIEwByID(R.ID.text_tem); TextVIEw text_location = findVIEwByID(R.ID.text_location); text_tem.setText(""); text_location.setText(""); text_name.setText(""); text_time.setText("\n\n" + getDate()); } //提交 public voID onClick(VIEw v) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); EditText ename = (EditText)findVIEwByID(R.ID.text_name); EditText etem = (EditText)findVIEwByID(R.ID.text_tem); EditText elocation = (EditText)findVIEwByID(R.ID.text_location); TextVIEw edate = (TextVIEw) findVIEwByID(R.ID.text_time); String name = ename.getText().toString(); String tem = etem.getText().toString(); String location = elocation.getText().toString(); String date = edate.getText().toString(); ContentValues cValue = new ContentValues(); cValue.put("name",name); cValue.put("tem",tem); cValue.put("location",location); cValue.put("date",date); sqliteDatabase db = dbHelper.getWritableDatabase(); db.insert("temtab",null,cValue); builder.setMessage("提交成功"); builder.setPositivebutton("确认", null); builder.create().show(); update(); } //动态请求权限 private voID initPermission() { //清空没有通过的权限 mPermissionList.clear(); //逐个判断是否还有未通过的权限 for (int i = 0; i < permissions.length; i++) { if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) { //添加还未授予的权限到mPermissionList中 mPermissionList.add(permissions[i]); } } //申请权限 if (mPermissionList.size() > 0) { //有权限没有通过,需要申请 ActivityCompat.requestPermissions(this, permissions, mRequestCode); } else { //权限已经通过,进行的后续 *** 作 } } /** * 动态请求权限的回调方法 * * @param requestCode 是我们自己定义的权限请求码 * @param permissions 是我们请求的权限名称数组 * @param grantResults 是我们在d出页面后是否允许权限的标识数组,数组的长度对应的是权限 * 名称数组的长度,数组的数据0表示允许权限,-1表示我们点击了禁止权限 */ @OverrIDe public voID onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); boolean hasPermissiondismiss = false;//有权限没有通过 if (mRequestCode == requestCode) { for (int i = 0; i < grantResults.length; i++) { if (grantResults[i] == -1) { hasPermissiondismiss = true; break; } } } if (hasPermissiondismiss) { //如果有未被允许的权限 showPermissionDialog(); } else { //权限已经都通过了,进行的后续 *** 作 } } /*不再提示权限时的展示对话框*/ AlertDialog mPermissionDialog; private voID showPermissionDialog() { if (mPermissionDialog == null) { mPermissionDialog = new AlertDialog.Builder(this) .setMessage("权限被禁止,请手动开启") .setPositivebutton("设置", new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog, int which) { cancelPermissionDialog(); //跳转到设置里去手动开启权限 Uri uri = Uri.fromParts("package", getPackagename(), null); Intent intent = new Intent(Settings.ACTION_APPliCATION_DETAILS_SETTINGS); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setData(uri); startActivity(intent); } }) .setNegativebutton("取消", new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog, int which) { //关闭页面或者做其他 *** 作 cancelPermissionDialog(); MainActivity.this.finish(); } }) .create(); } mPermissionDialog.show(); } private voID cancelPermissionDialog() { mPermissionDialog.cancel(); } public voID getLocation(VIEw v) { //获取位置的按钮 TextVIEw text_location = findVIEwByID(R.ID.text_location); text_location.setHint("正在获取位置..."); startLocate(); Log.v("MainActivity", "获取位置"); } /*定位 */ private voID startLocate() { mLocationClIEnt = new LocationClIEnt(this); //声明LocationClIEnt类 mLocationClIEnt.registerLocationListener(myListener); //注册监听函数 LocationClIEntoption option = new LocationClIEntoption(); option.setLocationMode(LocationClIEntoption.LocationMode.Hight_Accuracy); //可选,默认高精度,设置定位模式,高精度,低功耗,仅设备 option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系 option.setAddrType("all"); option.setScanSpan(0);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的 option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要 option.setopenGps(true);//可选,默认false,设置是否使用gps option.setLocationNotify(false);//可选,默认false,设置是否当GPS有效时按照1S/1次频率输出GPS结果 option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近” option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到 option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死 option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集 option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤GPS仿真结果,默认需要 mLocationClIEnt.setLocoption(option); //开启定位 mLocationClIEnt.start(); } private class MyLocationListener implements BDLocationListener { @OverrIDe public voID onReceiveLocation(BDLocation location) { StringBuffer sb = new StringBuffer(256); sb.append("time : "); sb.append(location.getTime()); sb.append("\nerror code : "); sb.append(location.getLocType()); sb.append("\nlatitude : "); sb.append(location.getLatitude()); sb.append("\nlontitude : "); sb.append(location.getLongitude()); sb.append("\nradius : "); sb.append(location.geTradius()); if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果 sb.append("\nspeed : "); sb.append(location.getSpeed());// 单位:公里每小时 sb.append("\nsatellite : "); sb.append(location.getSatelliteNumber()); sb.append("\nheight : "); sb.append(location.getAltitude());// 单位:米 sb.append("\ndirection : "); sb.append(location.getDirection());// 单位度 sb.append("\naddr : "); sb.append(location.getAddrstr()); sb.append("\ndescribe : "); sb.append("gps定位成功"); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果 sb.append("\naddr : "); sb.append(location.getAddrstr()); //运营商信息 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("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机"); } sb.append("\nlocationdescribe : "); sb.append(location.getLocationDescribe());// 位置语义化信息 List<Poi> List = location.getPoiList();// POI数据 if (List != null) { sb.append("\npoiList size = : "); sb.append(List.size()); for (Poi p : List) { sb.append("\npoi= : "); sb.append(p.getID() + " " + p.getname() + " " + p.getRank()); } } Log.e("描述:", sb.toString()); String addr = location.getAddrstr(); //获取详细地址信息 /*String country = location.getCountry(); //获取国家 String province = location.getProvince(); //获取省份 String city = location.getCity(); //获取城市 String district = location.getdistrict(); //获取区县 String street = location.getStreet(); //获取街道信息 String adcode = location.getAdCode(); //获取adcode String town = location.getTown(); //获取乡镇信息 */ TextVIEw text_location = findVIEwByID(R.ID.text_location); text_location.setHint("正在获取位置..."); text_location.setText(addr); } }}
DBHelper.java
package com.example.temp;import androID.content.Context;import androID.database.sqlite.*;public class DBHelper extends sqliteOpenHelper { public DBHelper(Context context){ super(context,"tem.db",null,3); } @OverrIDe public voID onCreate(sqliteDatabase sqliteDatabase) { sqliteDatabase.execsql("create table temtab(_ID INTEGER PRIMARY KEY autoINCREMENT, name text,tem text,location text,date text)"); } @OverrIDe public voID onUpgrade(sqliteDatabase sqliteDatabase,int oldVersion,int newVersion) { } @OverrIDe public voID onopen(sqliteDatabase sqliteDatabase){ super.onopen(sqliteDatabase); }}
布局文件activity_main.xml
<?xml version="1.0" enCoding="utf-8"?><androIDx.constraintlayout.Widget.ConstraintLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context=".MainActivity"> <EditText androID:ID="@+ID/text_tem" androID:layout_wIDth="227dp" androID:layout_height="86dp" androID:hint="体温" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constrainttop_toBottomOf="@+ID/text_name" /> <TextVIEw androID:ID="@+ID/text_time" androID:layout_wIDth="201dp" androID:layout_height="82dp" androID:textSize="18sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" tools:layout_editor_absoluteY="100dp" /> <EditText androID:ID="@+ID/text_name" androID:layout_wIDth="227dp" androID:layout_height="86dp" androID:hint="姓名" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constrainttop_toBottomOf="@+ID/text_time" /> <button androID:ID="@+ID/button_submit" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="提交" androID:onClick="onClick" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" app:layout_constrainttop_toBottomOf="@+ID/textVIEw7" /> <button androID:ID="@+ID/button_getlocate" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="获取位置" androID:onClick="getLocation" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" app:layout_constrainttop_toBottomOf="@+ID/text_location" /> <EditText androID:ID="@+ID/text_location" androID:layout_wIDth="247dp" androID:layout_height="105dp" androID:hint="位置" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constrainttop_toBottomOf="@+ID/text_tem" /> <TextVIEw androID:ID="@+ID/textVIEw7" androID:layout_wIDth="159dp" androID:layout_height="58dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constrainttop_toBottomOf="@+ID/button_getlocate" /></androIDx.constraintlayout.Widget.ConstraintLayout>
AndroIDManifest.xml
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.example.temp"> <!--用于进行网络定位--> <uses-permission androID:name="androID.permission.ACCESS_COARSE_LOCATION"></uses-permission> <!--用于访问GPS定位--> <uses-permission androID:name="androID.permission.ACCESS_FINE_LOCATION"></uses-permission> <!--获取运营商信息,用于支持提供运营商信息相关的接口--> <uses-permission androID:name="androID.permission.ACCESS_NETWORK_STATE"></uses-permission> <!--用于访问wifi网络信息,wifi信息会用于进行网络定位--> <uses-permission androID:name="androID.permission.ACCESS_WIFI_STATE"></uses-permission> <!--这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位--> <uses-permission androID:name="androID.permission.CHANGE_WIFI_STATE"></uses-permission> <!--用于访问网络,网络定位需要上网--> <uses-permission androID:name="androID.permission.INTERNET"></uses-permission> <application androID:allowBackup="true" androID:icon="@mipmap/ic_launcher" androID:label="体温上报器" androID:roundIcon="@mipmap/ic_launcher_round" androID:supportsRtl="true" androID:theme="@style/Apptheme"> <Meta-data androID:name="com.amap.API.v2.APIkey" androID:value="iBsqPf3mvdpGK9o1k4yeb15ge5b9GeLm"/> <activity androID:name=".MainActivity"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <service androID:name="com.baIDu.location.f" androID:enabled="true" androID:process=":remote"> </service> </application></manifest>
开发过程中遇到的难点:
1.动态权限的申请
要实现定位功能,需要APP向手机发送申请位置信息的权限。
2.定位的实现
利用百度地图SDK实现。
需要导入包
3.按钮功能的实现
利用OnClick=.....实现。
4.数据库的存储
此部分运用sqlite实现。
将数据库文件保存到本地用Navicat打开可实现可视化。
总结以上是内存溢出为你收集整理的Android开发-体温记录器全部内容,希望文章能够帮你解决Android开发-体温记录器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)