eclipse会变更SHA1码,如果地图显示不出来,检查SHA1码是否更改
-----------------------------------------------------------------------
MainActivity:
package com.aslan.simulategps.activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.widget.Toolbar.OnMenuItemClickListener;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.aslan.simulategps.R;
import com.aslan.simulategps.base.BaseActivity;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMapClickListener;
import com.baidu.mapapi.map.BaiduMap.OnMarkerDragListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
public class MainActivity extends BaseActivity implements LocationListener,
OnClickListener, BDLocationListener, OnMapClickListener,
OnMarkerDragListener, OnGetGeoCoderResultListener,
OnMenuItemClickListener {
private String mMockProviderName = LocationManager.GPS_PROVIDER;;//用于获取和调用定位服务。
private Button bt_Ok;
//LocationManager系统服务是位置服务的核心组件,它提供了一系列方法来处理与位置相关的问题,
//包括查询上一个已知位置、注册和注销来自某个LocationProvider的周期性的位置更新、注册和注销接近某个坐标时对一个已定义的Intent的触发等。
private LocationManager locationManager;
private double latitude = 31.3029742, longitude = 120.6097126;// 默认常州
private Thread thread;// 需要一个线程一直刷新
private Boolean RUN = true;
private TextView tv_location;
boolean isFirstLoc = true;// 是否首次定位
// 定位相关
private LocationClient mLocClient;
private LocationMode mCurrentMode;// 定位模式
private BitmapDescriptor mCurrentMarker;// 定位图标
private MapView mMapView;
private BaiduMap mBaiduMap;
// 初始化全局 bitmap 信息,不用时及时 recycle
private BitmapDescriptor bd = BitmapDescriptorFactory
.fromResource(R.drawable.icon_gcoding);
private Marker mMarker;
private LatLng curLatlng;
private GeoCoder mSearch;
private double myGpslatitude, myGpslongitude;
@Override
protected Object getContentViewId() {
return R.layout.activity_main;
}
@Override
protected void IniView() {
toolbar.setTitle("GPS欺骗");// 标题的文字需在setSupportActionBar之前,不然会无效
setSupportActionBar(toolbar);
bt_Ok = (Button) findViewById(R.id.bt_Ok);
tv_location = (TextView) findViewById(R.id.tv_location);
// 地图初始化
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
// 定位初始化
mLocClient = new LocationClient(this);
}
@Override
protected void IniLister() {
bt_Ok.setOnClickListener(this);
toolbar.setOnMenuItemClickListener(this);
mLocClient.registerLocationListener(this);
mBaiduMap.setOnMapClickListener(this);
mBaiduMap.setOnMarkerDragListener(this);
// 初始化搜索模块,注册事件监听
mSearch = GeoCoder.newInstance();
mSearch.setOnGetGeoCodeResultListener(this);
}
@Override
protected void IniData() {
inilocation();
iniMap();
}
/**
* iniMap 初始化地图
*
*/
private void iniMap() {
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);//一秒请求一次
mCurrentMode = LocationMode.NORMAL;//定位图层显示方式
// 缩放
MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(14.0f);
mBaiduMap.setMapStatus(msu);
//定位图层显示方式(定位图层显示方式,是否允许显示方向信息,用户自定义定位图标)
mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
mCurrentMode, true, mCurrentMarker));
mLocClient.setLocOption(option);//设置定位参数
mLocClient.start();//开始定位
initOverlay();
// 开启线程,一直修改GPS坐标
thread = new Thread(new Runnable() {
@Override
public void run() {
while (RUN) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
setLocation(longitude, latitude);
}
}
});
thread.start();
}
/**
* initOverlay 设置覆盖物,这里就是地图上那个点
*
*/
private void initOverlay() {
LatLng ll = new LatLng(latitude, longitude);
/**
* 设置覆盖物属性
* position 设置 marker 覆盖物的位置坐标
* icon 设置 Marker 覆盖物的图标
* zIndex 设置覆盖物的zIndex 类似图层 数值越大就显示在最上边
* draggable 设置 marker 是否允许拖拽,默认不可拖拽
*/
OverlayOptions oo = new MarkerOptions().position(ll).icon(bd).zIndex(9)
.draggable(true);
mMarker = (Marker) (mBaiduMap.addOverlay(oo));//添加
}
/**
* inilocation 初始化 位置模拟
*
*/
private void inilocation() {
//获取定位服务的实现类对象
locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
// 创建一个仿真位置信息并添加到当前正在运行的provider中
locationManager.addTestProvider(mMockProviderName, false, true, false,
false, true, true, true, 0, 5);
// 让创建好的仿真位置信息对于正在运行的provider可用。并且该值会替代原有真实provider中的数据
locationManager.setTestProviderEnabled(mMockProviderName, true);
//最小时间和最小距离
locationManager.requestLocationUpdates(mMockProviderName, 0, 0, this);
}
/**
* setLocation 设置GPS的位置
*
*/
private void setLocation(double longitude, double latitude) {
Location location = new Location(mMockProviderName);
location.setTime(System.currentTimeMillis());//返回当前的计算机时间
//获得经纬度
location.setLatitude(latitude);
location.setLongitude(longitude);
location.setAltitude(2.0f);
location.setAccuracy(3.0f);
locationManager.setTestProviderLocation(mMockProviderName, location);
}
/**
* 位置上的改变
*/
@Override
public void onLocationChanged(Location location) {
double lat = location.getLatitude();
double lng = location.getLongitude();
Log.i("gps", String.format("location: x=%s y=%s", lat, lng));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
public void onBackPressed() {
thisFinish();
}
@Override
protected void onDestroy() {
RUN = false;
thread = null;
// 退出时销毁定位
mLocClient.stop();
// 关闭定位图层
mBaiduMap.setMyLocationEnabled(false);
mMapView.onDestroy();
mMapView = null;
bd.recycle();
super.onDestroy();
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.bt_Ok:
latitude = curLatlng.latitude;
longitude = curLatlng.longitude;
break;
}
}
/**
* 定位SDK监听函数
*/
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null) {
return;
}
if (isFirstLoc) {
isFirstLoc = false;
myGpslatitude = location.getLatitude();
myGpslongitude = location.getLongitude();
LatLng ll = new LatLng(myGpslatitude, myGpslongitude);
setCurrentMapLatLng(ll);
}
}
@Override
public void onMapClick(LatLng arg0) {
setCurrentMapLatLng(arg0);
}
@Override
public boolean onMapPoiClick(MapPoi arg0) {
// TODO Auto-generated method stub
return false;
}
/**
* setCurrentMapLatLng 设置当前坐标
*/
private void setCurrentMapLatLng(LatLng arg0) {
curLatlng = arg0;
mMarker.setPosition(arg0);
// 设置地图中心点为这是位置
LatLng ll = new LatLng(arg0.latitude, arg0.longitude);
MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(u);
// 根据经纬度坐标 找到实地信息,会在接口onGetReverseGeoCodeResult中呈现结果
mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(arg0));
}
/**
* onMarkerDrag 地图上标记拖动结束
*/
@Override
public void onMarkerDrag(Marker arg0) {
// TODO Auto-generated method stub
}
/**
* onMarkerDragEnd 地图上标记拖动结束
*/
@Override
public void onMarkerDragEnd(Marker marker) {
setCurrentMapLatLng(marker.getPosition());
}
/**
* onMarkerDragStart 地图上标记拖动开始
*/
@Override
public void onMarkerDragStart(Marker arg0) {
// TODO Auto-generated method stub
}
/**
* onGetGeoCodeResult 搜索(根据实地信息-->经纬坐标)
*/
@Override
public void onGetGeoCodeResult(GeoCodeResult arg0) {
// TODO Auto-generated method stub
}
/**
* onGetReverseGeoCodeResult 搜索(根据坐标-->实地信息)
*/
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(this, "抱歉,未能找到结果", Toast.LENGTH_LONG).show();
return;
}
tv_location.setText(String.format("伪造位置:%s", result.getAddress()));
}
@Override
protected void thisFinish() {
AlertDialog.Builder build = new AlertDialog.Builder(this);
build.setTitle("提示");
build.setMessage("退出后,将不再提供定位服务,继续退出吗?");
build.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
build.setNeutralButton("最小化", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
moveTaskToBack(true);
}
});
build.setNegativeButton("取消", null);
build.show();
}
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.action_my:
LatLng ll = new LatLng(myGpslatitude, myGpslongitude);
setCurrentMapLatLng(ll);
break;
case R.id.action_about:
Intent intent = new Intent();
intent.setClass(this, AboutActivity.class);
startActivity(intent);
break;
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
----------------------------------------------------------------------我是一条你看不见的分割线-----------------------------------------------------------------------
AppApplication:
package com.aslan.simulategps;
import android.app.Application;
import com.baidu.mapapi.SDKInitializer;
public class AppApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// 在使用 SDK 各组间之前初始化 context 信息,传入 ApplicationContext
SDKInitializer.initialize(this);
}
}
----------------------------------------------------------------------我是一条你看不见的分割线-----------------------------------------------------------------------
修改后从网络获取地址并一直修改位置
package com.example.gpsdemo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SlidingDrawer;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.json.JSONObject;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMapClickListener;
import com.baidu.mapapi.map.BaiduMap.OnMarkerDragListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
public class MainActivity extends Activity implements LocationListener, OnClickListener,
BDLocationListener, OnMapClickListener, OnMarkerDragListener, OnGetGeoCoderResultListener {
private String mMockProviderName = LocationManager.GPS_PROVIDER;;
private Button bt_Ok;
private LocationManager locationManager;
private double latitude, longitude;// 默认常州
private Thread thread;// 需要一个线程一直刷新
private Boolean RUN = true;
private TextView tv_location;
boolean isFirstLoc = true;// 是否首次定位
// 定位相关
private LocationClient mLocClient;
private LocationMode mCurrentMode;// 定位模式
private BitmapDescriptor mCurrentMarker;// 定位图标
private MapView mMapView;
private BaiduMap mBaiduMap;
// 初始化全局 bitmap 信息,不用时及时 recycle
private BitmapDescriptor bd = BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding);
private Marker mMarker;
private LatLng curLatlng;
private GeoCoder mSearch;
private String model;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
iniView();
iniListner();
getJosn();
getHttp();
iniData();
home();
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* 回主屏
* @throws InterruptedException
*
*/
private void home() throws InterruptedException {
Thread.sleep(3000);
Intent intent= new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //如果是服务里调用,必须加入new task标识
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
/**
* iniView 界面初始化
*
*/
private void iniView() {
bt_Ok = (Button) findViewById(R.id.bt_Ok);
tv_location = (TextView) findViewById(R.id.tv_location);
getHttp();
// 地图初始化
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
// 定位初始化
mLocClient = new LocationClient(this);
}
/**
* iniListner 接口初始化
*
*/
private void iniListner() {
bt_Ok.setOnClickListener(this);
mLocClient.registerLocationListener(this);
mBaiduMap.setOnMapClickListener(this);
mBaiduMap.setOnMarkerDragListener(this);
// 初始化搜索模块,注册事件监听
mSearch = GeoCoder.newInstance();
mSearch.setOnGetGeoCodeResultListener(this);
}
/**
* iniData 数据初始化
*
*/
private void iniData() {
inilocation();
iniMap();
}
/**
* iniMap 初始化地图
*
*/
private void iniMap() {
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
mCurrentMode = LocationMode.NORMAL;
// 缩放
MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(14.0f);
mBaiduMap.setMapStatus(msu);
mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true,
mCurrentMarker));
mLocClient.setLocOption(option);
mLocClient.start();
initOverlay();
// 开启线程,一直修改GPS坐标
thread = new Thread(new Runnable() {
@Override
public void run() {
while (RUN) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
setLocation(longitude, latitude);
}
}
});
thread.start();
}
/**
* initOverlay 设置覆盖物,这里就是地图上那个点
*
*/
private void initOverlay() {
LatLng ll = new LatLng(latitude, longitude);
OverlayOptions oo = new MarkerOptions().position(ll).icon(bd).zIndex(9).draggable(true);
mMarker = (Marker) (mBaiduMap.addOverlay(oo));
}
/**
* inilocation 初始化 位置模拟
*
*/
private void inilocation() {
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.addTestProvider(mMockProviderName, false, true, false, false, true, true,
true, 0, 5);
locationManager.setTestProviderEnabled(mMockProviderName, true);
locationManager.requestLocationUpdates(mMockProviderName, 0, 0, this);
}
/**
* setLocation 设置GPS的位置
*
*/
private void setLocation(double longitude, double latitude) {
Location location = new Location(mMockProviderName);
location.setTime(System.currentTimeMillis());
location.setLatitude(latitude);
location.setLongitude(longitude);
location.setAltitude(2.0f);
location.setAccuracy(3.0f);
locationManager.setTestProviderLocation(mMockProviderName, location);
}
@Override
public void onLocationChanged(Location location) {
double lat = location.getLatitude();
double lng = location.getLongitude();
Log.i("gps", String.format("location: x=%s y=%s", lat, lng));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
public void onBackPressed() {
thisFinish();
}
@Override
protected void onDestroy() {
RUN = false;
thread = null;
// 退出时销毁定位
mLocClient.stop();
// 关闭定位图层
mBaiduMap.setMyLocationEnabled(false);
mMapView.onDestroy();
mMapView = null;
bd.recycle();
super.onDestroy();
}
private void thisFinish() {
AlertDialog.Builder build = new AlertDialog.Builder(this);
build.setTitle("提示");
build.setMessage("退出后,将不再提供定位服务,继续退出吗?");
build.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
build.setNeutralButton("最小化", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
moveTaskToBack(true);
}
});
build.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
build.show();
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.bt_Ok:
latitude = latitude;
longitude = longitude;
break;
}
}
/**
* 定位SDK监听函数
*/
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null) {
return;
}
if (isFirstLoc) {
isFirstLoc = false;
LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
setCurrentMapLatLng(ll);
}
}
@Override
public void onMapClick(LatLng arg0) {
setCurrentMapLatLng(arg0);
}
@Override
public boolean onMapPoiClick(MapPoi arg0) {
// TODO Auto-generated method stub
return false;
}
/**
* setCurrentMapLatLng 设置当前坐标
*/
private void setCurrentMapLatLng(LatLng arg0) {
// curLatlng = arg0;
mMarker.setPosition(arg0);
// 设置地图中心点为这是位置
LatLng ll = new LatLng(latitude, longitude);
MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(u);
// 根据经纬度坐标 找到实地信息,会在接口onGetReverseGeoCodeResult中呈现结果
mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(arg0));
}
/**
* onMarkerDrag 地图上标记拖动结束
*/
@Override
public void onMarkerDrag(Marker arg0) {
// TODO Auto-generated method stub
}
/**
* onMarkerDragEnd 地图上标记拖动结束
*/
@Override
public void onMarkerDragEnd(Marker marker) {
setCurrentMapLatLng(marker.getPosition());
}
/**
* onMarkerDragStart 地图上标记拖动开始
*/
@Override
public void onMarkerDragStart(Marker arg0) {
// TODO Auto-generated method stub
}
/**
* onGetGeoCodeResult 搜索(根据实地信息-->经纬坐标)
*/
@Override
public void onGetGeoCodeResult(GeoCodeResult arg0) {
// TODO Auto-generated method stub
}
/**
* onGetReverseGeoCodeResult 搜索(根据坐标-->实地信息)
*/
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(this, "抱歉,未能找到结果", Toast.LENGTH_LONG).show();
return;
}
tv_location.setText(String.format("伪造位置:%s", result.getAddress()));
}
/**
* JOSN解析获取手机标签
*/
public void getJosn() {
StringBuilder content = new StringBuilder(); // 文件内容字符串
String path = "mnt/sdcard/device_id.cfg";
File file = new File(path);
try {
FileInputStream fs = new FileInputStream(file);
InputStreamReader ir = new InputStreamReader(fs,"utf-8");
BufferedReader in = new BufferedReader(ir);
if (in != null) {
String line;
// 分行读取
while ((line = in.readLine()) != null) {
content.append(line);
}
in.close();
ir.close();
fs.close();
JSONObject root = new JSONObject(content.toString());
model = root.getString("wx_memo");
Toast.makeText(MainActivity.this, model, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
}
}
/**
* 获取坐标数据
*/
public void getHttp() {
String url = "http://weapp.f3322.net:8888/wemoney/index.php/home/Ditu/index/n/" + model;
new AsyncTask<String, Void, Void>(){
@Override
protected Void doInBackground(String... params) {
try {
URL url = new URL(params[0]);
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is,"utf-8");
BufferedReader br = new BufferedReader(isr);
String line;
String[] coordinate = null;
while((line = br.readLine()) != null){
coordinate = line.split("\\,");
}
br.close();
isr.close();
is.close();
longitude = Double.parseDouble(coordinate[0]);
latitude = Double.parseDouble(coordinate[1]);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}.execute(url);
}
}
----------------------------------------------------------------------我是一条你看不见的分割线-----------------------------------------------------------------------
开机自动启动:
BootBroadcastReceiver:
package com.example.gpsdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootBroadcastReceiver extends BroadcastReceiver {
static final String action_boot="android.intent.action.BOOT_COMPLETED";
/**
* 开机自动启动
*/
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(action_boot)){
Intent ootStartIntent=new Intent(context,MainActivity.class);
ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(ootStartIntent);
}
}
}