【问题标题】:AlertDialog is not getting disabled after enabling GPS location启用 GPS 定位后,AlertDialog 没有被禁用
【发布时间】:2019-01-04 05:20:25
【问题描述】:

我有一个带有自定义布局的警报对话框,当 GPS 或网络未启用时,它会弹出,以便用户启用它们。

代码如下:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    // Get Location Manager and check for GPS & Network location services
    final LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER) || !lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View mView = getLayoutInflater().inflate(R.layout.activity_gps_alert_dialog, null);
        Button positiveBtn = (Button) mView.findViewById(R.id.positiveBtn);
        final Button negativeBtn = (Button) mView.findViewById(R.id.negativeBtn);
        builder.setView(mView);
        final AlertDialog dialog = builder.create();
        positiveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Show location settings when the user acknowledges the alert dialog
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);

            }
        });

        negativeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                System.exit(0);
            }
        });
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

问题在于,即使启用了 GPS 和网络,即使用户通过它启用了这些弹出对话框,此弹出对话框仍在屏幕上。

【问题讨论】:

    标签: android location android-alertdialog


    【解决方案1】:

    用户按下肯定按钮后,您必须使用dialog.didmiss() 关闭对话框:

        positiveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Show location settings when the user acknowledges the alert dialog
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
                dialog.didmiss()
            }
        });
    

    对于另一部分,即使启用了 gps 和网络也会弹出对话框,请参阅以下帖子: isProviderEnabled(LocationManager.NETWORK_PROVIDER) return false isProviderEnabled(LocationManager.GPS_PROVIDER) always returns false in android 2.3 GPS isProviderEnabled always return false LocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) is not reliable, why?

    【讨论】:

    • 好的,谢谢,当用户按下按钮时它会关闭,但是如果启用了 gps 和网络,它仍然会在我使用应用程序午餐时弹出...对此有什么想法吗?
    • 我在任何地方都找不到我的答案:/谢谢你的时间我的朋友!
    • 如果您阅读了我提到的链接,您应该意识到您使用的 if 语句虽然符合逻辑但并不可靠。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    相关资源
    最近更新 更多