我在android工作。我正在设计一个基于我的设备的GPS位置的应用程序。
每当我在我的应用程序中按任何事件键时,我都需要检查我的设备是否一直在获取GPS位置。
请帮帮我。您可以为此提供网站链接。
提前谢谢。
答案 0 :(得分:2)
您应该使用这种类型的代码: -
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location!=null)
{
myLongitude = location.getLongitude();
myLatitude= location.getLatitude();
}
else
{
myLongitude =0;
myLatitude= 0;
}
LocationListener mlocListener;
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
Log.v("Checkin_Inspect_activity","cordintes of this place = "+myLatitude+" "+myLongitude);
设计你这样的课: -
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
myLatitude= loc.getLatitude();
myLongitude=loc.getLongitude();
}
@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)
{
}
}
答案 1 :(得分:0)
您可以使用LocationListener类,该类将在位置更新时提供位置详细信息
这是LocationListener的简单片段
LocationListener locationListener = new MyLocationListener();
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locationListener);
<强>被修改强>
您可以像这样检查提供商的gps位置
if (manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
// do something
}else{
// do another thing
}
查看本教程链接
http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/