第一次写安卓。。。为了大作业 只能用到什么学什么 作为初学者 深深的感受到安卓的恶意
环境sdk 都遇到不少问题
要用到地图定位 一开始先了解百度地图 各种不行 发现百度地图最新版本更新了 然后网上搜到的代码 都用不了
搞了很久 然后又尝试高德地图 又搞了很久还是不行。。。
用AndroidStudio中的虚拟机运行老是报错 提示安装不了apk
后来经别人提醒 使用手机运行 就可以了。。
使用高德地图 获取key什么的 就不在这详细说明 网上基本都有
但是好像那个发布版和调试版的SHA1值需要注意一下 应该是有区别的 这个我也不确定
下面是代码 可能会有jdk版本上面的问题 因为我写的时候用的是安卓7.0 或者其他问题。。。
AndroidManifest.xml
配置各种权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lenovo.bike">
<!-- 允许程序打开网络套接字 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 允许程序设置内置sd卡的写权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 允许程序获取网络状态 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 允许程序访问WiFi网络信息 -->
<!-- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> -->
<!-- 允许程序读写手机状态和身份 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- 允许程序访问CellID或WiFi热点来获取粗略的位置 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- 用于访问GPS定位 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位 -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<!-- 用于访问网络,网络定位需要上网 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 用于读取手机当前的状态 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- 写入扩展存储,向扩展卡写入数据,用于写入缓存定位数据 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="6e839a03339ec39f04b0d7255a47b9a1" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Map"
android:screenOrientation="portrait" />
<service android:name="com.amap.api.location.APSService" />
<activity android:name=".AlarmActivity"></activity>
</application>
</manifest>
MainActivity.java
package com.example.lenovo.bike;
import android.content.Context;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.content.Intent;
import android.widget.Toast;
import com.getpebble.android.kit.PebbleKit;
import com.getpebble.android.kit.util.PebbleDictionary;
import com.getpebble.android.kit.PebbleKit;
import com.getpebble.android.kit.util.PebbleDictionary;
import java.net.URI;
import java.net.URL;
//import android.os.Handler;
public class MainActivity extends AppCompatActivity {
Button button_1;
//button_1实现找车 按钮跳转到地图界面 根据当前读取到的自行车的经纬度进行定位显示
//计算当前位置与自行车的距离Len 把Len传给手表并显示
Button button_2;
//按下按钮2 获取当前自行车定位 作为初始值
//每5分钟读取一次服务器数据 与初始值进行比较 如何Len>50米 报警 给手表发送震动信号
TextView textView;
private PebbleKit.PebbleDataReceiver appMessageReciever;//lynn communication between pebble
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//button_1响应
button_1=(Button)findViewById(R.id.button1);
button_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent();
intent.setClass(MainActivity.this,Map.class);
startActivity(intent);
}
});
//button_2响应
button_2=(Button)findViewById(R.id.button2);
button_2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
// URL url=new URL("http://");
double init_lat,init_lon;//按下按钮获取当前自行车位置信息
//每一分钟从服务器读取一次数据
Intent intent=new Intent();
intent.setClass(MainActivity.this,AlarmActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onResume() {
super.onResume();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.lenovo.bike.MainActivity">
<Button
android:layout_width="match_parent"
android:text="AntiTheft"
android:id="@+id/button2"
android:layout_below="@+id/button1"
android:layout_alignParentStart="true"
android:layout_marginTop="50dp"
android:layout_marginLeft="85dp"
android:layout_marginRight="85dp"
android:layout_height="60dp"
android:textAllCaps="false"/>
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="FindBike"
android:id="@+id/button1"
android:layout_marginTop="60dp"
android:layout_marginLeft="85dp"
android:layout_marginRight="85dp"
android:layout_alignParentTop="true"
android:textAllCaps="false"/>
<!--<TextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="activity_main 的 xml"-->
<!--android:id="@+id/textView"-->
<!--android:layout_alignParentTop="true"-->
<!--android:layout_alignParentLeft="true"-->
<!--android:layout_alignParentStart="true" />-->
</RelativeLayout>
Map.java
package com.example.lenovo.bike;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.*;
import com.amap.api.maps.LocationSource.OnLocationChangedListener;
import com.amap.api.maps.SupportMapFragment;
import com.amap.api.maps.AMap;
import com.amap.api.maps.UiSettings;
import com.amap.api.location.AMapLocation;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
public class Map extends AppCompatActivity{
private MapView mapView;
private AMap aMap;
public AMapLocationClient mLocationClient=null;
//声明mLocationOption对象
public AMapLocationClientOption mLocationOption = null;
// double bike_lat;
// double bike_lon;//测试使用设置默认值
private double lat;
private double lon;
MarkerOptions markerOptions=null;
// MarkerOptions BikeOptions=null;
double Latitude,Longitude;
//定位回调监听器
public AMapLocationListener mLocationListener=new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation amapLocation) {
if(amapLocation!=null){
if(amapLocation.getErrorCode()==0){
//定位成功回调信息,设置相关消息
amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
amapLocation.getLatitude();//获取纬度
amapLocation.getLongitude();//获取经度
amapLocation.getAccuracy();//获取精度信息
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(amapLocation.getTime());
df.format(date);//定位时间
amapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。
amapLocation.getCountry();//国家信息
amapLocation.getProvince();//省信息
amapLocation.getCity();//城市信息
amapLocation.getDistrict();//城区信息
amapLocation.getStreet();//街道信息
amapLocation.getStreetNum();//街道门牌号信息
amapLocation.getCityCode();//城市编码
amapLocation.getAdCode();//地区编码
amapLocation.getAoiName();//获取当前定位点的AOI信息
lat = amapLocation.getLatitude();
lon = amapLocation.getLongitude();
Log.v("pcw","lat : "+lat+" lon : "+lon);
//设置mark所在位置为自行车
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon), 18));
// if(markerOptions!=null||BikeOptions!=null){
// aMap.clear();
// }
if(markerOptions!=null){
aMap.clear();
}
markerOptions = new MarkerOptions();
markerOptions.position(new LatLng(lat, lon));
markerOptions.title("我的位置");
markerOptions.visible(true);
aMap.addMarker(markerOptions);
// LatLng people_latlng=new LatLng(lat,lon);
//
// bike_lat=40.00;
// bike_lon=116.33;//测试使用设置默认值
bike_lat=bike_lat+0.001;
bike_lon=bike_lon+0.001;
// LatLng bike_latlng=new LatLng(bike_lat,bike_lon);
// double distance=AMapUtils.calculateLineDistance(people_latlng,bike_latlng);
// int Len=(int)distance;//将len的值发给手表
// Log.v("Distance","Distance : "+distance);
// aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(bike_lat, bike_lon), 18));
// BikeOptions = new MarkerOptions();
// BikeOptions.position(new LatLng(bike_lat, bike_lon));
// BikeOptions.title("自行车位置");
// BikeOptions.visible(true);
//
// aMap.addMarker(BikeOptions);
}else{
//显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
Log.e("AmapError", "location Error, ErrCode:"
+ amapLocation.getErrorCode() + ", errInfo:"
+ amapLocation.getErrorInfo());
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
//初始化地图控件
mapView = (MapView) findViewById(R.id.map);
//必须要写
mapView.onCreate(savedInstanceState);
//初始化定位
mLocationClient = new AMapLocationClient(getApplicationContext());
//设置定位回调监听
mLocationClient.setLocationListener(mLocationListener);
init();
}
private void init(){
if(aMap==null){
aMap=mapView.getMap();
}
setUpMap();
}
private void setUpMap() {
//初始化定位参数
mLocationOption = new AMapLocationClientOption();
//设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//设置是否返回地址信息(默认返回地址信息)
mLocationOption.setNeedAddress(true);
//设置是否只定位一次,默认为false
mLocationOption.setOnceLocation(false);
//设置是否强制刷新WIFI,默认为强制刷新
mLocationOption.setWifiActiveScan(true);
//设置是否允许模拟位置,默认为false,不允许模拟位置
mLocationOption.setMockEnable(false);
//设置定位间隔,单位毫秒,默认为2000ms
mLocationOption.setInterval(18000);
//给定位客户端对象设置定位参数
mLocationClient.setLocationOption(mLocationOption);
//启动定位
mLocationClient.startLocation();
}
/**
* 方法必须重写
*/
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
/**
* 方法必须重写
*/
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onStop() {
super.onStop();
mLocationClient.stopLocation();//停止定位
}
/**
* 方法必须重写
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
/**
* 方法必须重写
*/
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
mLocationClient.onDestroy();
}
}
// lat : 40.001205 lon : 116.335021
activity_map.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.lenovo.bike.Map">
<com.amap.api.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!--<fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.amap.api.maps.SupportMapFragment">-->
<!--</fragment>-->
<!--<RelativeLayout-->
<!--android:layout_width="fill_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_gravity="top"-->
<!--android:orientation="vertical" >-->
<!--<Button-->
<!--android:id="@+id/btn_back"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_centerVertical="true"-->
<!--android:text="返回"-->
<!--android:textColor="#ffffff"-->
<!--android:textSize="15sp" />-->
<!--<TextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_centerInParent="true"-->
<!--android:text="我的位置"-->
<!--android:textColor="#ffffff"-->
<!--android:textSize="20.0sp" />-->
<!--</RelativeLayout>-->
<!--<LinearLayout-->
<!--android:id="@+id/layout_loading"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:background="#000000"-->
<!--android:gravity="center"-->
<!--android:orientation="vertical" >-->
<!--<ProgressBar-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content" />-->
<!--<TextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_marginTop="10dp"-->
<!--android:text="正在确定你的位置..."-->
<!--android:textColor="#ffffff"-->
<!--android:textSize="20sp" />-->
<!--</LinearLayout>-->
<!--<Button android:id="@+id/bt_button1"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="获取海拔" />-->
<!--<TextView android:id="@+id/tv_textview1"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="具体信息"-->
<!--android:layout_below="@id/bt_button1" />-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is map"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
AlarmActivity.java //这个里面基本没实现什么功能了 虽然代码很多 但是没用 为了完整性还是贴出来了
package com.example.lenovo.bike;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import java.util.Timer;
import java.util.TimerTask;
public class AlarmActivity extends AppCompatActivity {
// private ImageView IV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
// IV = (ImageView) findViewById(R.id.Bike);
// IV.setImageResource(R.drawable.bikesafe);
final int Len=45;
//
// int Len=45;
// boolean flag=true;
// while(flag){
// Len++;
IV.setImageResource(R.drawable.bikesafe);
// try{
// Thread.sleep(3000);
// System.out.println("Hello !!!");
// System.out.println("Len "+Len);
// if(Len>50)
// flag=false;
// }catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// if(!flag){
// System.out.println("Len "+Len);
//
// }
// }
//
IV.setImageResource(R.drawable.biketheft);
//
//
// new Thread() {
//
// int Len=45;
// public void run() {
// boolean flag=true;
// while(flag){
// try {
// Len++;
// System.out.println("Hello !!!");
// System.out.println("Len "+Len);
// Thread.sleep(3000);
// if(Len>50)
// flag=false;
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
//
//
// }
//
// }.start();
//
Timer timer = new Timer();
long delay = 0;
long intevalPeriod = 10 * 1000;
// schedules the task to be run in an interval
timer.scheduleAtFixedRate(task, delay, intevalPeriod);
//
//
if(Len>50) {
LynnPebbleInteraction.sendVibrate(getApplicationContext(),2);
}
//
//
}
//
// protected void onPause() {
// super.onPause();
// new Thread() {
private ImageView IV;
//
// int Len=45;
// public void run() {
// try {
//
//
// Len++;
// System.out.println("Hello !!!");
// System.out.println("Len "+Len);
// if(Len<50){
//
// }else{
// IV.setImageResource(R.drawable.biketheft);
// }
// Thread.sleep(3000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
//
// }
//
// }.start();
// }
}
activity_alarm.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_alarm"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.lenovo.bike.AlarmActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter The AntiTheft Mode..."
android:gravity="center_horizontal"
android:id="@+id/textView"
android:textColor="@color/red"
android:textSize="25sp"
android:textStyle="bold"
android:layout_marginTop="49dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:id="@+id/Bike"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true" />
<!--<ImageView-->
<!--android:id="@+id/BikeTheft"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:src="@drawable/biketheft"-->
<!--android:layout_marginTop="40dp"-->
<!--android:layout_below="@+id/textView"-->
<!--android:layout_alignParentStart="true" />-->
<!--<ImageView-->
<!--android:id="@+id/BikeSafe"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:src="@drawable/bikesafe"-->
<!--android:layout_below="@+id/textView"-->
<!--android:layout_alignParentStart="true" />-->
</RelativeLayout>
就贴这么多吧。。。主要是地图模块的代码。。。
经过这次大作业 我觉的 这东西 不适合我
弃了