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 ...
随机推荐
- PO*创建标准采购订单
-- l_iface_rec po_headers_interface%ROWTYPE; 校验头相关信息 ) INTO l_po_count FROM po_headers_all p ...
- webservice系统学习笔记7-异常处理
接口类:IMyService.java @WebResult(name="testExceptionResult") public void testException() thr ...
- &和&&的一点区别
&两边的语句都会执行,&&有可能只执行左边的语句. &做位运算与的时候可以更高效的判断一个数字是不是奇数. 1&1 = 1 0&1 = 0 所以和1与结 ...
- 【asp.net Core 2.0 初步探索】
首先下载 对应的SDK 和runtime https://www.microsoft.com/net/core#linuxubuntu ---------当前为 1.1 稳定版本 ...
- 升级openssh基于openssl
OpenSSH is the premier connectivity tool for remote login with the SSH protocol. It encrypts all tra ...
- 让MyEclipse支持mac的Retina屏解决字体模糊的问题
最近在使用MyEclipse时最让我头疼的问题就是MyEclipse并不能很好的支持苹果的Retina屏幕,看着字体会非常模糊,那叫一个心酸,真白瞎了这块好屏幕.(不知道最新的版本有没有支持,反正我的 ...
- 工具:SVN的Web客户端(ViewVC、SVNWebClient、sventon)和任务管理(Trac、Collaboa)
http://www.blogjava.net/evanwhj/archive/2006/04/06/39498.aspx 在前面一篇文章中,痛诉了安装ViewVC for Subversion的种种 ...
- 禁用root登录以及使用sudo分配权限
前言 最近一段时间服务器有受到一些挖比特币的人的攻击,这里就简要说明一下提升服务器安全度的做法 禁用root登录 root权限可以说是linux服务器的最高权限,如果我们平常都是直接使用root用户来 ...
- HDUOJ---1233还是畅通工程
还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- RabbitMQ消息队列的小伙伴: ProtoBuf(Google Protocol Buffer) [转]
什么是ProtoBuf? 一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化.它很适合做数据存储或 RPC 数据交换格式.可用于通讯协议.数据存储等领域的语言无关.平台无关.可扩 ...