如何查看Android设备的GPS是否启用

在启用Android Cupcake(1.5)的设备上,如何检查并激活GPS?

最好的办法似乎如下:

final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE ); if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) { buildAlertMessageNoGps(); } private void buildAlertMessageNoGps() { final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Your GPS seems to be disabled, do you want to enable it?") .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) { startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) { dialog.cancel(); } }); final AlertDialog alert = builder.create(); alert.show(); } 

在android中,我们可以使用LocationManager轻松检查设备中是否启用了GPS。

这是一个简单的程序来检查。

GPS是否启用: – 将AndroidManifest.xml中的以下用户权限行添加到访问位置

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

你的java类文件应该是

 public class ExampleApp extends Activity { /** Called when the activity is first created. */ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ Toast.makeText(this, "GPS is Enabled in your devide", Toast.LENGTH_SHORT).show(); }else{ showGPSDisabledAlertToUser(); } } private void showGPSDisabledAlertToUser(){ AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?") .setCancelable(false) .setPositiveButton("Goto Settings Page To Enable GPS", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int id){ Intent callGPSSettingIntent = new Intent( android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(callGPSSettingIntent); } }); alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int id){ dialog.cancel(); } }); AlertDialog alert = alertDialogBuilder.create(); alert.show(); } } 

输出将如下所示

在这里输入图像描述

在这里输入图像描述

是的,GPS设置不能以编程方式更改,因为它们是隐私设置,我们必须检查它们是否打开或不从程序打开,如果它们没有打开,则进行处理。 您可以通知用户GPS已关闭,并使用类似的方法将设置屏幕显示给用户。

检查位置提供商是否可用

  String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if(provider != null){ Log.v(TAG, " Location providers: "+provider); //Start searching for location and update the location text when update available startFetchingLocation(); }else{ // Notify users and show settings if they want to enable GPS } 

如果用户想要启用GPS,您可以用这种方式显示设置屏幕。

 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivityForResult(intent, REQUEST_CODE); 

在你的onActivityResult中,你可以看到用户是否启用了它

  protected void onActivityResult(int requestCode, int resultCode, Intent data){ if(requestCode == REQUEST_CODE && resultCode == 0){ String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if(provider != null){ Log.v(TAG, " Location providers: "+provider); //Start searching for location and update the location text when update available. // Do whatever you want startFetchingLocation(); }else{ //Users did not switch on the GPS } } } 

这是做到这一点,我希望它有帮助。 让我知道如果我做错了什么。

这里是步骤:

第1步:创建在后台运行的服务。

步骤2:您还需要Manifest文件的以下权限:

 android.permission.ACCESS_FINE_LOCATION 

第3步:编写代码:

  final LocationManager manager = (LocationManager)context.getSystemService (Context.LOCATION_SERVICE ); if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) Toast.makeText(context, "GPS is disabled!", Toast.LENGTH_LONG).show(); else Toast.makeText(context, "GPS is enabled!", Toast.LENGTH_LONG).show(); 

第4步:或者只是你可以检查使用:

 LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE ); boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

步骤5:不断运行您的服务来监视连接。

是的,你可以检查下面的代码是:

 public boolean isGPSEnabled (Context mContext){ LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); } 

如果用户允许在其设置中使用GPS,则将使用GPS。

你不能再明确地改变这一点,但你不必 – 这是一个真正的隐私设置,所以你不想调整它。 如果用户确定应用程序获得了精确的坐标,它将会开启。 然后,如果可以,位置管理器API将使用GPS。

如果没有GPS,您的应用程序确实没有用处,并且它已关闭,则可以使用意图在右侧屏幕上打开设置应用程序,以便用户启用它。

在您的LocationListener ,实现onProviderEnabledonProviderDisabled事件处理程序。 当您调用requestLocationUpdates(...) ,如果GPS在手机上被禁用, onProviderDisabled将被调用; 如果用户启用GPS, onProviderEnabled将被调用。

这段代码检查GPS状态

 final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE ); if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) { buildAlertMessageNoGps(); } 

`