安卓学习之学生签到APP(一)

 一、学生定位签到页面

 

具体实现步骤:

1.1 高德地图申请key

1、创建新应用

进入高德地图api控制台,创建一个新应用。如果您之前已经创建过应用,可直接跳过这个步骤。

2、添加新Key

在创建的应用上点击"添加新Key"按钮,在弹出的对话框中,依次:输入应用名名称,选择绑定的服务为“Android平台SDK”,输入发布版安全码  SHA1、调试版安全码 SHA1、及 Package,如下图所示:

 

 安卓学习之学生签到APP(一)_第1张图片

 

附:SHA1码的获取

 

1.2 实现高德地图自动定位---准备工作

1、下载jar包,导入jar包

  首先去高德地图API官网下载好相关SDK jar文件,将下载的定位 SDK jar 文件复制到工程(此处截图以官方示例Demo为例子)的 libs 目录下,如果有老版本定位 jar 文件存在,请删除。

注:

  • 第一个是2D地图的jar包,因为最后定位以后我要在地图上标出来位置
  • 第二个是用于定位的jar包

对于每个jar文件,右键-选择Add As Library,导入到工程中。或者使用菜单栏 选择 File ->Project Structure->Modules-> Dependencies。点击绿色的加号选择File dependency. 然后选择要添加的jar包即可,此时build.gradle中会自动生成如下信息。 

  

2、配置AndroidMainfest.xml文件

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

<application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"

        >
        
        <meta-data
            android:name="com.amap.api.v2.apikey"
            android:value="这里填写第一步你申请的那个key" />
        
        <service android:name="com.amap.api.location.APSService">service>
application>
AndroidMainfest.xml

 

3、Activity的布局文件
<com.amap.api.maps2d.MapView
        android:id="@+id/map_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    com.amap.api.maps2d.MapView>

    <Button
        android:id="@+id/requet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="签到"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
activity_main

 

4.

  1 package com.cqvie.android_map;
  2 
  3 import android.content.DialogInterface;
  4 import android.icu.text.SimpleDateFormat;
  5 import android.os.Bundle;
  6 import android.os.Handler;
  7 import android.os.Message;
  8 import android.support.v7.app.AlertDialog;
  9 import android.support.v7.app.AppCompatActivity;
 10 import android.util.Log;
 11 import android.view.View;
 12 import android.widget.Button;
 13 import android.widget.Toast;
 14 
 15 import com.amap.api.location.AMapLocation;
 16 import com.amap.api.location.AMapLocationClient;
 17 import com.amap.api.location.AMapLocationClientOption;
 18 import com.amap.api.location.AMapLocationListener;
 19 import com.amap.api.maps2d.AMap;
 20 import com.amap.api.maps2d.CameraUpdateFactory;
 21 import com.amap.api.maps2d.LocationSource;
 22 import com.amap.api.maps2d.MapView;
 23 import com.amap.api.maps2d.UiSettings;
 24 import com.amap.api.maps2d.model.BitmapDescriptorFactory;
 25 import com.amap.api.maps2d.model.LatLng;
 26 import com.amap.api.maps2d.model.MarkerOptions;
 27 import com.cqvie.options.HttpUtilsHttpURLConnection;
 28 
 29 import org.json.JSONException;
 30 import org.json.JSONObject;
 31 
 32 import java.util.Date;
 33 import java.util.HashMap;
 34 import java.util.Map;
 35 
 36 
 37 public class MainActivity extends AppCompatActivity implements LocationSource, AMapLocationListener {
 38 
 39     private MapView mapView = null;
 40     private AMap aMap;
 41     private Button request;
 42     private AMapLocationClient mLocationClient;
 43     private AMapLocationClientOption mLocationOption;
 44     private OnLocationChangedListener mListener;
 45     //标识,用于判断是否只显示一次定位信息和用户重新定位
 46     private boolean isFirstLoc = true;
 47     String date=null;
 48 
 49     @Override
 50     protected void onCreate(Bundle savedInstanceState) {
 51         super.onCreate(savedInstanceState);
 52         setContentView(R.layout.activity_main);
 53 
 54         //获取地图控件引用
 55         mapView = (MapView) findViewById(R.id.map_view);
 56         //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),实现地图生命周期管理
 57         mapView.onCreate(savedInstanceState);
 58         //获取签到按钮
 59         request = (Button) findViewById(R.id.requet);
 60 
 61         init();
 62         //初始化定位
 63         mLocationClient = new AMapLocationClient(getApplicationContext());
 64         mLocationClient.setLocationListener(this);
 65         //初始化定位参数
 66         mLocationOption = new AMapLocationClientOption();
 67         // mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Device_Sensors);
 68         mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
 69         //设置是否返回地址信息(默认返回地址信息)
 70         mLocationOption.setNeedAddress(true);
 71         //设置是否只定位一次
 72         mLocationOption.setOnceLocation(true);
 73         //设置是否强制刷新WiFi
 74         mLocationOption.setWifiScan(true);
 75         //设置定位间隔,单位毫秒,默认为2000ms
 76         //mLocationOption.setInterval(2000);
 77         //给定位客户端对象设置定位参数
 78         mLocationClient.setLocationOption(mLocationOption);
 79         //启动定位
 80         mLocationClient.startLocation();
 81     }
 82 
 83     private void init() {
 84         if (aMap == null) {
 85             aMap = mapView.getMap();
 86             UiSettings settings = aMap.getUiSettings();
 87             aMap.setLocationSource(this);
 88             settings.setMyLocationButtonEnabled(true);
 89             aMap.setMyLocationEnabled(true);
 90         }
 91     }
 92 
 93 
 94     @Override
 95     protected void onDestroy() {
 96         super.onDestroy();
 97         //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
 98         mapView.onDestroy();
 99         //mLocationClient.stopLocation();
100         mLocationClient.onDestroy();
101     }
102 
103     @Override
104     protected void onResume() {
105         super.onResume();
106         //在activity执行onResume时执行mMapView.onResume (),实现地图生命周期管理
107         mapView.onResume();
108     }
109 
110     @Override
111     protected void onPause() {
112         super.onPause();
113         //在activity执行onPause时执行mMapView.onPause (),实现地图生命周期管理
114         mapView.onPause();
115     }
116 
117     @Override
118     protected void onSaveInstanceState(Bundle outState) {
119         super.onSaveInstanceState(outState);
120         //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),实现地图生命周期管理
121         mapView.onSaveInstanceState(outState);
122     }
123 
124     //    @RequiresApi(api = Build.VERSION_CODES.N)
125     @Override
126     public void onLocationChanged(final AMapLocation aMapLocation) {
127 
128         if (aMapLocation!=null){
129             if (aMapLocation.getErrorCode()==0){
130                 //定位成功回调信息,设置相关消息
131                 aMapLocation.getLocationType();
132                 aMapLocation.getLatitude();//获取纬度
133                 aMapLocation.getLongitude();//获取经度
134                 aMapLocation.getAccuracy();//获取精度信息
135                 SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
136                 Date nowdate = new Date(aMapLocation.getTime());
137                 date = df.format(nowdate);//定位时间
138                 aMapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。
139                 aMapLocation.getCountry();//国家信息
140                 aMapLocation.getProvince();//省信息
141                 aMapLocation.getCity();//城市信息
142                 aMapLocation.getDistrict();//城区信息
143                 aMapLocation.getStreet();//街道信息
144                 aMapLocation.getStreetNum();//街道门牌号信息
145                 //aMapLocation.getBuildingId();//获取当前室内定位的建筑物Id
146                 //aMapLocation.getAccuracy();//获取精度信息
147                 aMapLocation.getPoiName();
148                 aMapLocation.getAoiName();
149 
150                 //Log.d("why", String.valueOf(aMapLocation.getAoiName()));
151 //                if (isFirstLoc){
152                 //设置缩放级别
153                 aMap.moveCamera(CameraUpdateFactory.zoomTo(17));
154                 //将地图移动到定位点
155                 aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude())));
156                 //点击定位按钮 能够将地图的中心移动到定位点
157                 mListener.onLocationChanged(aMapLocation);
158                 //获取定位信息
159                 final StringBuffer buffer = new StringBuffer();
160                 buffer.append(aMapLocation.getCountry() + ""
161                         //+ aMapLocation.getProvince() + ""
162                         + aMapLocation.getCity() + ""
163                         // + aMapLocation.getProvince() + ""
164                         + aMapLocation.getDistrict() + ""
165                         + aMapLocation.getStreet() + ""
166                         + aMapLocation.getStreetNum());
167 
168 
169                 Toast.makeText(getApplicationContext(), buffer.toString() + aMapLocation.getAoiName(), Toast.LENGTH_LONG).show();
170                 //Log.d("tzk",buffer.toString());
171 
172                 isFirstLoc = false;
173             }
174         }
175         else {
176             Log.d("why", String.valueOf(aMapLocation.getErrorCode()));
177             //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
178             Log.e("AmapError", "location Error, ErrCode:"
179                     + aMapLocation.getErrorCode() + ", errInfo:"
180                     + aMapLocation.getErrorInfo());
181             Toast.makeText(getApplicationContext(), "定位失败", Toast.LENGTH_LONG).show();
182         }
183 
184         request.setOnClickListener(new View.OnClickListener() {
185             @Override
186             public void onClick(View view) {
187                 new AlertDialog.Builder(MainActivity.this)
188                         .setTitle("签到")
189                         .setMessage(date+ "\n" + aMapLocation.getAoiName())
190                         .setPositiveButton("确定?", new DialogInterface.OnClickListener() {
191                             @Override
192                             public void onClick(DialogInterface dialogInterface, int i) {
193                                 //新页面接收数据
194                                 Bundle bundle = MainActivity.this.getIntent().getExtras();
195                                 //接收name值
196                                 String name = bundle.getString("name");
197                                 //Toast.makeText(MainActivity.this,date,Toast.LENGTH_LONG).show();
198                                 signClickListen(date,name,aMapLocation.getAoiName());
199 
200                             }
201                         })
202                         .show();
203             }
204         });
205     }
206 
207     //激活定位
208     @Override
209     public void activate(OnLocationChangedListener onLocationChangedListener) {
210         mListener = onLocationChangedListener;
211     }
212 
213     @Override
214     public void deactivate() {
215         mListener = null;
216     }
217 
218     /**
219      * 签到点击事件
220      */
221     public void signClickListen(final String date,final  String name,final String signaddress) {
222         new Thread(new Runnable() {
223             @Override
224             public void run() {
225                 String url = HttpUtilsHttpURLConnection.BASE_URL + "/SignServlet";
226                 Map params = new HashMap();
227                 Message msg = new Message();
228                 Bundle data = new Bundle();
229                 params.put("signtime",date);
230                 params.put("name", name);
231                 params.put("signaddress", signaddress);
232                 String result = HttpUtilsHttpURLConnection.getContextByHttp(url, params);
233                 msg.what = 0x12;
234                 data.putString("result", result);
235                 msg.setData(data);
236                 hander.sendMessage(msg);
237             }
238             Handler hander = new Handler() {
239                 @Override
240                 public void handleMessage(Message msg) {
241                     if (msg.what == 0x12) {
242                         Bundle data = msg.getData();
243                         String key = data.getString("result");//得到json返回的json
244 //                                   Toast.makeText(MainActivity.this,key,Toast.LENGTH_LONG).show();
245                         try {
246                             JSONObject json = new JSONObject(key);
247                             String result = (String) json.get("result");
248                             if ("success".equals(result)) {
249                                 Toast.makeText(MainActivity.this,name + "签到成功!", Toast.LENGTH_SHORT).show();
250 
251                             } else if ("error".equals(result)) {
252                                 Toast.makeText(MainActivity.this,name + "签到失败!", Toast.LENGTH_SHORT).show();
253                             }
254                         } catch (JSONException e) {
255                             e.printStackTrace();
256                         }
257                     }
258                 }
259             };
260         }).start();
261     }
262 }
MainActivity

 

转载于:https://www.cnblogs.com/tanzk/p/6932322.html

你可能感兴趣的:(安卓学习之学生签到APP(一))