这是我的代码:
主要服务类
public class SimpleService extends Service {
Timer mytimer;
private String provider;
//LocationManager locationManager;
//LocationListener mlocListener;
//private Handler toasthandler = new Handler();
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
mytimer = new Timer();
mytimer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
toastHandler.sendEmptyMessage(0);
}
},0,1000);
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
mytimer.cancel();
}
private final Handler toastHandler=new Handler()
{
public void handleMessage(Message msg)
{
getdata();
};
};
public void getdata()
{
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
mlocListener);
// Initialize the location fields
}
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " + "Latitude = "
+ loc.getLatitude() + "Longitude = " + loc.getLongitude();
Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT)
.show();
Log.d("TAG", "Starting..");
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Disabled",
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Enabled",
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
} //End of Class MyLocationListener */
}
这里我在服务中使用计时器在每1秒后调用requestLocationUpdate()
方法,但是此方法不会被调用。当我从DDMS按下按钮时,一次打印所有TAG.D。
log cat中的输出如下所示:
05-11 15:47:29.763: DEBUG/dalvikvm(308): GC_EXPLICIT freed 47 objects / 2264 bytes in 59ms
05-11 15:47:34.904: DEBUG/dalvikvm(190): GC_EXPLICIT freed 88 objects / 3856 bytes in 151ms
05-11 15:47:36.294: DEBUG/TAG(3677): Starting..
05-11 15:47:36.294: DEBUG/TAG(3677): Starting..
05-11 15:47:36.294: DEBUG/TAG(2449): Starting..
05-11 15:47:36.524: DEBUG/TAG(3677): Starting..
05-11 15:47:36.534: DEBUG/TAG(3677): Starting..
05-11 15:47:36.544: DEBUG/TAG(3677): Starting..
05-11 15:47:36.554: DEBUG/TAG(3677): Starting..
05-11 15:47:36.564: DEBUG/TAG(3677): Starting..
05-11 15:47:36.574: DEBUG/TAG(3677): Starting..
05-11 15:47:36.594: DEBUG/TAG(3677): Starting..
05-11 15:47:36.604: DEBUG/TAG(3677): Starting..
05-11 15:47:36.614: DEBUG/TAG(3677): Starting..
05-11 15:47:36.624: DEBUG/TAG(3677): Starting..
05-11 15:47:36.635: DEBUG/TAG(3677): Starting..
05-11 15:47:36.644: DEBUG/TAG(3677): Starting..
05-11 15:47:36.664: DEBUG/TAG(3677): Starting..
05-11 15:47:36.674: DEBUG/TAG(3677): Starting..
05-11 15:47:36.694: DEBUG/TAG(3677): Starting..
05-11 15:47:36.704: DEBUG/TAG(3677): Starting..
05-11 15:47:36.714: DEBUG/TAG(3677): Starting..
05-11 15:47:36.734: DEBUG/TAG(3677): Starting..
05-11 15:47:44.394: DEBUG/dalvikvm(58): GREF has increased to 401
05-11 15:47:47.734: DEBUG/dalvikvm(2449): GC_EXPLICIT freed 372 objects / 20424 bytes in 149ms
05-11 15:47:53.663: DEBUG/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol
你能否告诉我代码中出现了什么问题?
答案 0 :(得分:1)
您无需使用计时器来请求位置更新。 注册接收位置更新后,您将收到有关每个位置更改事件的通知。
考虑以下示例:
public class SimpleService extends Service {
Timer mytimer;
private String provider;
LocationListener mlocListener = new LocationListener {
@Override
public void onLocationChanged(Location loc) {
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " + "Latitude = "
+ loc.getLatitude() + "Longitude = " + loc.getLongitude();
Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT)
.show();
Log.d("TAG", "Starting..");
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Enabled",
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
} //End of Class MyLocationListener */;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
mlocListener );
}
@Override
public void onDestroy() {
super.onDestroy();
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mlocManager.removeUpdates(mlocListener);
Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
}
}