MyLocationService
package com.baidu.location.service; import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; import com.baidu.baidulocationdemo.R;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.Poi;
import com.baidu.location.demo.LocationActivity;
import com.baidu.location.demo.LocationApplication; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map; import javax.net.ssl.HttpsURLConnection; /**
* Created by admin on 2016/7/18.
*/
public class MyLocationService extends Service {
private LocationService locationService;
public static String workid; @Override
public IBinder onBind(Intent intent) {
return null;
} @Override
public void onCreate() {
super.onCreate();
Log.i("warn", "oncreate");
} @Override
public void onStart(Intent intent, int startid) {
super.onStart(intent, startid);
// -----------location config ------------
locationService = ((LocationApplication) getApplication()).locationService;
//获取locationservice实例,建议应用中只初始化1个location实例,然后使用,可以参考其他示例的activity,都是通过此种方式获取locationservice实例的
locationService.registerListener(mListener);
//注册监听
Log.i("warn", "onstart");
int type = intent.getIntExtra("from", 0);
if (type == 0) {
locationService.setLocationOption(locationService.getDefaultLocationClientOption());
} else if (type == 1) {
locationService.setLocationOption(locationService.getOption());
}
MyLocationService.workid = intent.getStringExtra("workid");
locationService.start();
} @Override
public void onDestroy() {
Log.i("warn", "ondestroy");
locationService.stop(); //停止定位服务
locationService.unregisterListener(mListener); //注销掉监听
} public BDLocationListener mListener = new BDLocationListener() { @Override
public void onReceiveLocation(BDLocation location) {
// TODO Auto-generated method stub
if (null != location && location.getLocType() != BDLocation.TypeServerError) { final String longitude, latitude, jingdu, speed, city;
//String time = location.getTime();
Date date = new Date();
final long timestamp = date.getTime(); //时间
longitude = Double.toString(location.getLongitude()); //经度
latitude = Double.toString(location.getLatitude()); //纬度
jingdu = Float.toString(location.getRadius()); //精度
city = location.getCity();
speed = Float.toString(location.getSpeed()); //gps下运行速度
final String workid = MyLocationService.workid;
Log.i("workid", workid); new Thread() {
@Override
public void run() {
httpget(workid, timestamp, longitude, latitude, jingdu, city, speed);
}
}.start(); }
} }; public String httpget(String workid, long time, String longitude, String latitude, String jingdu, String city, String speed) { String result = "";
BufferedReader in = null;
StringBuilder buf = new StringBuilder("http://www.agribiotech.cn/record/record/sizerecord");
buf.append("?");
buf.append("workid=" + workid + "&");
buf.append("timestamp=" + time + "&");
buf.append("lng=" + longitude + "&");
buf.append("lat=" + latitude + "&");
buf.append("radius=" + jingdu + "&");
buf.append("city=" + URLEncoder.encode(city) + "&");
buf.append("speed=" + speed); try {
URL url = null;
url = new URL(buf.toString());
URLConnection conn = url.openConnection();
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestProperty("Accept", "application/json");
conn.connect();
Map<String, List<String>> map = conn.getHeaderFields();
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += "\n" + line;
} } catch (IOException e) {
Log.i("warn", e.toString());
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}
MyLocationService的更多相关文章
- 判断Service是否在运行
private boolean isServiceRunning() { ActivityManager manager = (ActivityManager) getSystemService(AC ...
- static使用实例
public class LocationActivity extends Activity { //一个Activity传值到service public static String workid ...
- LocationActivity
package com.baidu.location.demo; import com.baidu.baidulocationdemo.R;import com.baidu.location.BDLo ...
随机推荐
- 类的专有方法(__len__)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #http://www.imooc.com/code/6252 #类的专有方法(__len__) #如果一个类 ...
- Loadrunner脚本回放 场景运行过程中常见错误分析
问题一:Loadrunner超时错误问题描述 Loadrunner超时错误:在录制Web协议脚本回放时超时情况经常出现,产生错误的原因也有很多,解决的方法也不同. 问题现象Error -27728: ...
- iOS-仿京东6位密码支付输入框
概述 用于安全支付的密码支付输入框. 详细 代码下载:http://www.demodashi.com/demo/10709.html 开发需求中有时候我们需要用于安全支付的功能, 需要设置APP钱包 ...
- (CF#257)B. Jzzhu and Sequences
Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...
- (一)Linux实操之——权限、任务调度、磁盘分区
1. 权限 1.1 查看权限 通过ls -l命令可以看到文件的详细信息 下面以一条信息解释各个位置字符的作用 -rwxr--r--. 1 root root 32 6月 18 10:15 choose ...
- 使用mysql遇到的异常
Can't find any matching row in the user table 原因 更改mysql.user表之后,没有使用flush privileges命令来更新权限. 解决方案 使 ...
- Numpy库应用实例——GPS定位
背景介绍 定位系统 GPS全球定位系统(Global Positioning System)以GPS系统为例介绍卫星定位的计算方法 GPS定位的基本原理 GPS定位的基本原理是根据高速运动卫星的 ...
- linux创建新用户及权限
在Linux中添加普通新用户 ,超级用户(也称为“root”)是一个具有修改系统中任何文件权力的特别账号.在日常工作中,最好不要使用超级用户账号进入系统,因为任何错误操作都可能导致巨大的损失.由于超级 ...
- su: user tomcat does not exist
http://www.cnblogs.com/allegro/p/5005352.html 问题在于 你的startup.sh 里面设置了 用户,你需要修改为root或者tomcat用户 这是开发 迁 ...
- .net/c# RabbitMQ 连接断开处理-断线重连(转载)
Rabbitmq 官方给的NET consumer示例代码如下,但使用过程,会遇到connection断开的问题,一旦断开,这个代码就会报错,就会导致消费者或者生产者挂掉. 下图是生产者发送消息,我手 ...