[英]Android :How can i check whether GPS is working in my android device or not?
我在android工作。 我正在設計一個基於我的設備的GPS位置的應用程序。
每當我在我的應用程序中按任何事件鍵時,我需要檢查我的設備是否一直在獲取GPS位置。
請幫幫我。 您可以為此提供網站鏈接。
先感謝您。
你應該使用這種類型的代碼: -
/* 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)
{
}
}
您可以使用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/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.