个人作业体温上报

体温上报系统

                                        

 

项目结构大概如下图所示

 

这个应该是我接触的第一个android的项目了,也算是我编程以来第一个独自完成的作品吧,做完这个项目感觉自己成长了不少,也使我对android产生了一些兴趣,总体来说收获还是挺大的。

代码展视(只展示部分代码)

 

package com.example.tem_report_2.location;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.example.tem_report_2.Activity.index_activity;
import com.example.tem_report_2.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.Poi;
import com.example.tem_report_2.MainActivity;
import com.example.tem_report_2.R;
import com.example.tem_report_2.location.LocationService;

import java.util.ArrayList;
import java.util.List;


public class LocationActivity extends AppCompatActivity {

   private Button btnLocationInsert;
  private EditText locationInfo ;

   LocationClient mLocationClient;
   MapView mMapView;
   BaiduMap mBaiduMap = null;


   //判断是否是第一次
   boolean isFirstLocate = true;
   boolean isFirstLocate2 = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);






        SDKInitializer.initialize(getApplicationContext());

        setContentView(R.layout.activity_location);

        locationInfo = findViewById(R.id.tv_locationInfo);
        btnLocationInsert = findViewById(R.id.bt_location_insert);
        mLocationClient = new LocationClient(getApplicationContext());
        mLocationClient.registerLocationListener(new MyLocationListener());

        mMapView = findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();

        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);

        mBaiduMap.setMyLocationEnabled(true);

        System.out.println("999");

        List<String> permissionList = new ArrayList<String>();

        if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
            permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
        }
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)!= PackageManager.PERMISSION_GRANTED){
            permissionList.add(Manifest.permission.READ_PHONE_STATE);
        }
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
            permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        if(!permissionList.isEmpty()){
            String [] permissions = permissionList.toArray(new String[permissionList.size()]);
            ActivityCompat.requestPermissions(this,permissions,1);
        }else {
            requestLocation();
        }

//        Intent intent1 =getIntent();
//        /*取出Intent中附加的数据*/
//        String first = intent1.getStringExtra("str1");
//        System.out.println("9999999");
//        if(first!=null){
//            String etstrpos = locationInfo.getText().toString();
//            Intent intent2 = new Intent(LocationActivity.this, index_activity.class);
//            intent2.putExtra("locationInfo", etstrpos);
//            startActivity(intent2);
//        }

        btnLocationInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LocationActivity.this,index_activity.class);

                String et1Str = locationInfo.getText().toString();
               // intent.putExtra("etPos", et1Str);
                intent.putExtra("msg",et1Str);
                setResult(200,intent);

               //startActivity(intent);
                finish();
            }
        });


    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 1:
                if(grantResults.length>0){
                    for (int result :grantResults){
                        if(result != PackageManager.PERMISSION_GRANTED){
                            Toast.makeText(this,"必须同意所有权限才能使用本程序",Toast.LENGTH_SHORT).show();
                            finish();
                            return;
                        }
                    }
                    requestLocation();
                }else {
                    Toast.makeText(this,"发生未知问题",Toast.LENGTH_SHORT).show();
                }
        }
    }

    private void requestLocation(){
        initLocation();
        mLocationClient.start();
    }

    private void initLocation(){
        LocationClientOption option = new LocationClientOption();
        //高精度定位模式
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);

        //返回经纬度坐标类型
        option.setCoorType("bd0911");

        //发起请求定位的间隔
        option.setScanSpan(1000);

        //设置时候使用gps
        option.setOpenGps(true);


        option.setLocationNotify(true);

        option.setIgnoreKillProcess(false);

        option.SetIgnoreCacheException(false);

        option.setWifiCacheTimeOut(5 * 60 * 1000);

        option.setEnableSimulateGps(false);

        //显示具体位置信息
        option.setIsNeedAddress(true);


        option.setAddrType("all");

        mLocationClient.setLocOption(option);
    }


    public class MyLocationListener extends BDAbstractLocationListener{

        @Override
        public void onReceiveLocation(BDLocation location) {
           if(isFirstLocate2){
               navigateTo(location);
               StringBuilder currentPosition = new StringBuilder();
               // currentPosition.append("纬度:").append(location.getLatitude()).append("\n");
               // currentPosition.append("经度:").append(location.getLongitude()).append("\n");
               currentPosition.append(location.getCountry());
               currentPosition.append(location.getProvince());
               currentPosition.append(location.getCity());
               currentPosition.append(location.getDistrict());
               currentPosition.append(location.getTown());
               currentPosition.append(location.getStreet());
               //currentPosition.append(location.getAddrStr());
               //currentPosition.append("定位方式:");
//            if(location.getLocType()== BDLocation.TypeGpsLocation){
//                currentPosition.append("GPS");
//            }else if(location.getLocType() == BDLocation.TypeNetWorkLocation){
//                currentPosition.append("网络");
               System.out.println("1111");
//            }
               //输出地理位置
               currentPosition.toString();
               System.out.println(currentPosition);
               locationInfo.setText(currentPosition);
               isFirstLocate2 = false;
           }


        }
    }

    //传地址到index_activity

    //使地图显示并放大
    private void navigateTo(BDLocation location){
        if(isFirstLocate){
            LatLng ll = new LatLng(location.getLatitude(),location.getLongitude());
            MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll);
            mBaiduMap.animateMapStatus(update);
            update = MapStatusUpdateFactory.zoomTo(16f);
            mBaiduMap.animateMapStatus(update);
            isFirstLocate = false;
        }
           // 显示自身位置
        MyLocationData.Builder locationBulider = new MyLocationData.Builder();
        locationBulider.longitude(location.getLongitude());
        locationBulider.latitude(location.getLatitude());
        MyLocationData locationData = locationBulider.build();
        mBaiduMap.setMyLocationData(locationData);
    }

    //activity生命周期相关
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
        mBaiduMap.setMyLocationEnabled(false);
        mLocationClient.stop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }
}

 

 

package com.example.tem_report_2.location;

import android.content.Context;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;

public class LocationService {
    private LocationClient client = null;
    private LocationClientOption mOption,DIYoption;
    private Object objLock = new Object();

    public LocationService(Context locationContext){
        synchronized (objLock) {
            if(client == null){
                client = new LocationClient(locationContext);
                client.setLocOption(getDefaultLocationClientOption());
            }
        }
    }

    // 注册
    public boolean registerListener(BDAbstractLocationListener listener){
        boolean isSuccess = false;
        if(listener != null){
            client.registerLocationListener(listener);
            isSuccess = true;
        }
        return  isSuccess;
    }

    // 注销
    public void unregisterListener(BDAbstractLocationListener listener){
        if(listener != null){
            client.unRegisterLocationListener(listener);
        }
    }

    //设置配置
    public boolean setLocationOption(LocationClientOption option){
        boolean isSuccess = false;
        if(option != null){
            if(client.isStarted())
                client.stop();
            DIYoption = option;
            client.setLocOption(option);
        }
        return isSuccess;
    }

    //默认Option设置
    public LocationClientOption getDefaultLocationClientOption(){
        if(mOption == null){
            mOption = new LocationClientOption();
            mOption.setLocationMode(LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
            mOption.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系,如果配合百度地图使用,建议设置为bd09ll;
            mOption.setScanSpan(3000);//可选,默认0,即仅定位一次,设置发起连续定位请求的间隔需要大于等于1000ms才是有效的
            mOption.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
            mOption.setIsNeedLocationDescribe(true);//可选,设置是否需要地址描述
            mOption.setNeedDeviceDirect(false);//可选,设置是否需要设备方向结果
            mOption.setLocationNotify(false);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
            mOption.setIgnoreKillProcess(true);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
            mOption.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于在北京天安门附近
            mOption.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
            mOption.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集
            mOption.setOpenGps(true);//可选,默认false,设置是否开启Gps定位
            mOption.setIsNeedAltitude(false);//可选,默认false,设置定位时是否需要海拔信息,默认不需要,除基础定位版本都可用

        }
        return mOption;
    }


    //自定义Option设置
    public LocationClientOption getOption(){
        if(DIYoption == null) {
            DIYoption = new LocationClientOption();
        }
        return DIYoption;
    }

    public void start(){
        synchronized (objLock) {
            if(client != null && !client.isStarted()){
                client.start();
            }
        }
    }
    public void stop(){
        synchronized (objLock) {
            if(client != null && client.isStarted()){
                client.stop();
            }
        }
    }

    public boolean isStart() {
        return client.isStarted();
    }

    public boolean requestHotSpotState(){
        return client.requestHotSpotState();
    }
}

 

posted @ 2021-06-20 21:20  见怪见外  阅读(65)  评论(0编辑  收藏  举报