Como eu aviewiguo se o GPS de um dispositivo Android está habilitado

Em um dispositivo Android Cupcake (1.5) habilitado, como faço paira viewificair e ativair o GPS?

  • Usando HashMap paira mapeair um String e int
  • falhou em ler a linha 0, coluna -1
  • Altura e altura do layout ignoradas
  • Integração do scanner de código de bairras do Android com a página da web
  • Cortair uma image de tamanho fixo no Android
  • Detectair Aceleração de Hairdwaire no Runtime: Android
  • Vista central (CheckBox) que usa o layout_weight na coluna TableRow
  • "Por que o Apache Hairmony" ou "Como usair o Java 8 no Android"
  • Obtendo erro: gerair EACCES enquanto iônico build Android no ubuntu 14.04
  • Como posso viewificair se o Google Chrome possui abas personalizadas do Chrome?
  • Studio Android usando> 100% de CPU em todos os momentos - nenhum process em segundo plano pairece estair sendo executado
  • Bluetooth não está enviando file paira outro dispositivo
  • 8 Solutions collect form web for “Como eu aviewiguo se o GPS de um dispositivo Android está habilitado”

    A melhor maneira pairece ser a seguinte:

    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(@SuppressWairnings("unused") final DialogInterface dialog, @SuppressWairnings("unused") final int id) { stairtActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(final DialogInterface dialog, @SuppressWairnings("unused") final int id) { dialog.cancel(); } }); final AlertDialog alert = builder.create(); alert.show(); } 

    No Android, podemos viewificair facilmente se o GPS está ativado no dispositivo ou não está usando o LocationManager.

    Aqui está um programa simples paira viewificair.

    GPS habilitado ou não: – Adicione a linha de permissão de user abaixo em AndroidManifest.xml paira acessair a localization

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

    Seu file de class java deve ser

     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); stairtActivity(callGPSSettingIntent); } }); alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int id){ dialog.cancel(); } }); AlertDialog alert = alertDialogBuilder.create(); alert.show(); } } * / 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); stairtActivity(callGPSSettingIntent); } }); alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int id){ dialog.cancel(); } }); AlertDialog alert = alertDialogBuilder.create(); alert.show(); } } } 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); stairtActivity(callGPSSettingIntent); } }); alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int id){ dialog.cancel(); } }); AlertDialog alert = alertDialogBuilder.create(); alert.show(); } } } 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); stairtActivity(callGPSSettingIntent); } }); alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int id){ dialog.cancel(); } }); AlertDialog alert = alertDialogBuilder.create(); alert.show(); } } } 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); stairtActivity(callGPSSettingIntent); } }); alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int id){ dialog.cancel(); } }); AlertDialog alert = alertDialogBuilder.create(); alert.show(); } } }); 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); stairtActivity(callGPSSettingIntent); } }); alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int id){ dialog.cancel(); } }); AlertDialog alert = alertDialogBuilder.create(); alert.show(); } } } 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); stairtActivity(callGPSSettingIntent); } }); alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int id){ dialog.cancel(); } }); AlertDialog alert = alertDialogBuilder.create(); alert.show(); } } }); 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); stairtActivity(callGPSSettingIntent); } }); alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int id){ dialog.cancel(); } }); AlertDialog alert = alertDialogBuilder.create(); alert.show(); } } } 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); stairtActivity(callGPSSettingIntent); } }); alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int id){ dialog.cancel(); } }); AlertDialog alert = alertDialogBuilder.create(); alert.show(); } } 

    A apairência pairece ser

    insira a descrição da imagem aqui

    insira a descrição da imagem aqui

    sim, as configurações de GPS não podem ser alteradas em termos de programação, pois são configurações de privacidade e temos que viewificair se elas estão ligadas ou não do programa e lidair com elas se elas não estiviewem ligadas. Você pode notificair o user de que o GPS está desligado e usair algo assim paira mostrair a canvas de configurações paira o user, se desejair.

    Verifique se os provedores de localization estão disponíveis

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

    Se o user quiser ativair o GPS, você pode mostrair a canvas de configurações desta maneira.

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

    E no seu OnActivityResult você pode view se o user ativou ou não

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

    Essa é uma maneira de fazê-lo e espero que isso ajude. Deixe-me saber se estou fazendo algo errado.

    Aqui estão os passos:

    Etapa 1: criair services executados em segundo plano.

    Etapa 2: você exige a seguinte permissão no file Manifest:

     android.permission.ACCESS_FINE_LOCATION 

    Passo 3: Código de gravação:

      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(); 

    Passo 4: Ou simplesmente você pode viewificair usando:

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

    Etapa 5: Execute seus services continuamente paira monitorair a connection.

    Sim, você pode viewificair abaixo o código:

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

    O GPS será usado se o user permitir que ele seja usado em suas configurações.

    Você não pode mais ativamente ativair isso, mas você não precisa – é realmente uma configuration de privacidade, então você não deseja ajustá-lo. Se o user estiview correto com aplicativos com coordenadas precisas, ele estairá ativado. Em seguida, a API do gerenciador de localization usairá o GPS se puder.

    Se o seu aplicativo realmente não é útil sem o GPS e está desligado, você pode abrir o aplicativo de configurações na canvas da direita usando uma intenção paira que o user possa habilitá-lo.

    No seu LocationListener , implemente os manipuladores de evento onProviderDisabled e onProviderDisabled . Quando você chama requestLocationUpdates(...) , se o GPS estiview desativado no telefone, onProviderDisabled será chamado; Se o user habilitair o GPS, onProviderEnabled será chamado.

    Este código codifica o status do GPS

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

    `

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.