标签:自己 注册 名称 over 离线 bool amp ppc ignore
首先是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 是我们在弹出页面后是否允许权限的标识数组,数组的长度对应的是权限 * 名称数组的长度,数组的数据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打开可实现可视化。
标签:自己 注册 名称 over 离线 bool amp ppc ignore
原文地址:https://www.cnblogs.com/a8047/p/14446413.html