有时候我的按钮无法在我的应用中运行

时间:2015-03-27 09:35:34

标签: android gps android-button

我正在检查GPS是否已启用点击按钮,有时GPS启用时间也是我的按钮不起作用。如果我关闭gps并再次手动打开GPS,那么它就能正常工作。

我该如何解决这个问题?

这是我的GPS代码

/*
 * checking gps is available or not in android device.
 */
@SuppressWarnings("deprecation")
public boolean isGPSAvailable() {
    ContentResolver contentResolver = getBaseContext().getContentResolver();
    boolean isGPSOn = Settings.Secure.isLocationProviderEnabled(
            contentResolver, LocationManager.GPS_PROVIDER);
    if (!isGPSOn) {
        new DialogHelper(this, R.string.title_err_attention,
                R.string.msg_err_nogps, R.string.txt_settings,
                R.string.txt_exit, new DialogCallBack() {

                    @Override
                    protected void onPositiveBtnClick() {
                        // TODO Auto-generated method stub
                        super.onPositiveBtnClick();
                        Intent settings = new Intent(
                                Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivity(settings);
                        Home.this.finish();
                    }

                    @Override
                    protected void onNegativeBtnClick() {
                        // TODO Auto-generated method stub
                        super.onNegativeBtnClick();
                        Home.this.finish();
                    }
                }).show();
    }

    return isGPSOn;
}

2 个答案:

答案 0 :(得分:0)

使用类似的布尔值: isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

答案 1 :(得分:0)

我已经制作了一种检查GPS状态的方法。它可能会对你有帮助。

public static boolean isGpsAvailable(Context context) {
            LocationManager manager = (LocationManager) context
                    .getSystemService(Context.LOCATION_SERVICE);
            boolean statusOfGPS = manager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
            return statusOfGPS;
        }