2010.12.27——— android service
16lz
2021-01-23
2010.12.27——— android service
参考:http://apps.hi.baidu.com/share/detail/23409277
需求:在应用开启时,启动一个service 不断的获得当前的gps数据
代码:
service:
清单文件:
调用:
总结:
参考:http://apps.hi.baidu.com/share/detail/23409277
需求:在应用开启时,启动一个service 不断的获得当前的gps数据
代码:
service:
package com.huitu.project;import java.io.IOException;import org.apache.http.client.ClientProtocolException;import com.huitu.util.HttpUtil;import android.app.Service;import android.content.Context;import android.content.Intent;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.os.IBinder;public class GPSService extends Service{private String user_id;@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubLocationManager locationManager; locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);user_id = intent.getStringExtra("user_id");String provider = LocationManager.GPS_PROVIDER;Location location = locationManager.getLastKnownLocation(provider);updateMap(location,user_id);LocationListener ll = new LocationListener(){public void onLocationChanged(Location location) {updateMap(location,user_id);}public void onProviderDisabled(String provider) {}public void onProviderEnabled(String provider) {}public void onStatusChanged(String provider, int status,Bundle extras) {} };locationManager.requestLocationUpdates(provider, 500, 5,ll);return null;}@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();}@Overridepublic void onStart(Intent intent, int startId) {// TODO Auto-generated method stubsuper.onStart(intent, startId);}private void updateMap(Location location,String user_id){System.out.println("gps run");double lat=0.0;double lng = 0.0;if (location != null) { lat = location.getLatitude(); lng = location.getLongitude();}String url = HttpUtil.BASE_URL+"android_addGPS_M.action?lat="+lat+"&lng="+lng+"&user_id="+user_id;try {HttpUtil.getHttpResponse(HttpUtil.getHttpPost(url));} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
清单文件:
<service android:name=".GPSService"></service>
调用:
Intent service = new Intent(this,GPSService.class);service.putExtra("user_id", result[1]);intent.putExtra("user_id", result[1]);intent.putExtra("org_id", result[2]);//startService(service);bindService(service, null, BIND_AUTO_CREATE);
总结:
- startService:应用退出了 仍然运行 比如说mp3的播放
- bindService 应用退出 也就结束了
更多相关文章
- Android商城开发(一)——一次活动页需求引发的危机感
- 清单文件Manifest中的android:name
- Android 官方文档:(二)应用清单 —— 2.2 <action>标签
- Android6.0 MTK 需求文档(五)
- Android 官方文档:(二)应用清单 —— 2.26 <uses-permission>标签
- Android开发学习清单
- Android清单文件属性大全
- android Mainifest权限设置清单
- Android实现录屏直播(二)需求才是硬道理之产品功能调研