2010.12.27——— android 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 应用退出 也就结束了


更多相关文章

  1. Android(安卓)Usb源码分析
  2. android应用程序组件之间的通讯
  3. android 学习五 设置应用程序全屏(没有状态栏和标题栏)
  4. android 常用资料
  5. 2010.12.27——— android service
  6. Android(安卓)Binder进程间通信-ServiceManager代理对象的获取过
  7. Android(安卓)和 H5 互调
  8. android intent和intent action大全
  9. android 模拟器横竖切换

随机推荐

  1. 【Android(安卓)Training视频系列】第4讲
  2. 免费学习android开发技术分享
  3. android:installLocation简析
  4. android 开发BUG
  5. android设定手机的显示模式,横竖屏,是否全
  6. Android多国语言文件夹命名
  7. Android——通知(Notification)
  8. Android当中的SeekBar与iOS中的UISlider
  9. Android(安卓)path 贝塞尔曲线 波浪形
  10. 在android jni中使用log