【问题标题】:GPS location is enabled but can't find device locationGPS 定位已启用,但无法找到设备位置
【发布时间】:2021-05-30 13:31:47
【问题描述】:

应用程序卡在白色对话框中,即使在启用位置后也是如此。 如下所述,有两个文件用于定位责任。


  1. RemindActivity.java:
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.remind_activity);
  locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
.
.
.
        LocationPremissionCheck();
        GooglePlayServiceCheck();
        GPSLocationServiceCheck();
.
.
.
}
 private void GPSLocationServiceCheck() {
        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Your GPS seems to be disabled, enable it to use this app?")
                    .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();
                            Intent intent = new Intent(RemindActivity.this, StartActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                            finish();

                        }
                    });
            final AlertDialog alert = builder.create();
            alert.show();
        }
    }


上面同样的东西写在RemindActivity.java中如下:

  1. RemindActivity.java:
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.remind_activity);
  locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
.
.
.
        LocationPremissionCheck();
        GooglePlayServiceCheck();
        GPSLocationServiceCheck();
.
.
.
}
 private void GPSLocationServiceCheck() {
        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Your GPS seems to be disabled, enable it to use this app?")
                    .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();
                            Intent intent = new Intent(RemindActivity.this, StartActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                            finish();

                        }
                    });
            final AlertDialog alert = builder.create();
            alert.show();
        }
    }

这是一个注册屏幕,此位置适用于某些设备,但不适用于所有设备(正如我测试的那样)。 应用程序卡在白色对话框中,即使在启用位置后也是如此。

如何解决此问题。 提前致谢。

【问题讨论】:

  • 使用服务不断检查Gps。
  • @androidLearner 如何做到这一点兄弟...请指导我

标签: java android android-studio location


【解决方案1】:

如果您只是想检查应用程序在前台时是否启用了位置,请使用以下方法。

一旦您启动位置源设置,关闭对话框并在您的活动中显示一些刷新按钮。当您点击刷新按钮时,再次重新创建活动。

     public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                            startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                             finish();
                        }

     //in your activity
        public void onRefresh() {
             reCreate();
            }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多