在Android中检索服务中的位置更新
我尝试检索网络提供商提供的服务中的位置更新,但这些更新无法执行。我的代码如下所示:在Android中检索服务中的位置更新,android,service,location,Android,Service,Location,我尝试检索网络提供商提供的服务中的位置更新,但这些更新无法执行。我的代码如下所示: public class TimeService extends Service{ LocationManager lm; LocationListener ll = new LocationListener() { @Override public void onLocationChanged(Location location) { double latitude = l
public class TimeService extends Service{
LocationManager lm;
LocationListener ll = new LocationListener()
{
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longtitude = location.getLongitude();
Intent inte =new Intent( TimeService.this, myAppWidgetProvider.class);
inte.setAction("action_location");
inte.putExtra("lat", latitude);
inte.putExtra("long", longtitude);
PendingIntent pinte = PendingIntent.getBroadcast(TimeService.this, 0, inte, 0);
try {
pinte.send();
} catch (CanceledException e) {
// TODO Auto-generated catch block
Log.d("sendPIException", ":-(");
}
Log.d("locReceived", "onLochanged");
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
};
onLocationChanged中的日志“locReceived”在logcat中未显示
以下代码也负责我的服务中的位置:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.registerReceiver(br, new IntentFilter(Intent.ACTION_TIME_TICK));
lm =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);
return super.onStartCommand(intent, flags, startId);
}
我的代码出了什么问题?检查清单上是否有适当的权限。我有访问\u粗略\u位置、访问\u精细\u位置和internet的权限,并且我也在清单中声明了服务。请检查这一项,不要恨我,但您是否使用startService()启动您的服务?是:)我的服务函数中有每分钟更新时间的功能,它正在工作。请尝试使用
this.getApplicationContext()
代替this
行lm=(LocationManager)this.getSystemService(Context.LOCATION\u服务)
试试我的最后一个选项,顺便说一句,它可以将日志添加到所有4个其他方法中:onProviderEnabled
。。。。
Criteria criteria = new Criteria();
String provider = lm.getBestProvider(criteria, false);
Location location = lm.getLastKnownLocation(provider);
lm.requestLocationUpdates(provider, 0, 0, ll);
if (location == null) {
Log.e("Impl", "Location not available");
}