检查Android上是否启用了GPS

时间:2016-09-28 18:11:04

标签: java android android-gps

现在,我正在使用一个代码来检查"位置服务"启用但我真正需要的是检查"使用GPS卫星"选中选项是因为"使用无线网络"对我的应用程序不起作用。

Image that shows GPS enabled

MainActivity.java

 final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
    if ( !manager.isProviderEnabled( LocationManager.NETWORK_PROVIDER ) ) {

        if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
        HomeFragment.displayPromptForEnablingGPS(this);}
    }

HomeFragment.java

public static void displayPromptForEnablingGPS(
        final Activity activity)
{
    final AlertDialog.Builder builder =
            new AlertDialog.Builder(activity);
    final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
    final String message = "Não foi possível localiza-lo."
            + " Por favor habilite o GPS ou qualquer"
            + " outro serviço de localização para encontrar"
            + " as acompanhantes próximas de você.";


    builder.setMessage(message)
            .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface d, int id) {
                            activity.startActivity(new Intent(action));
                            System.exit(0);
                            d.dismiss();
                        }
                    });

    builder.create().show();


}

0 个答案:

没有答案