启用或禁用时,Android会在按钮上显示GPS状态

时间:2014-02-14 11:58:00

标签: android gps

如果GPS已关闭且应用程序启动了一个警告框,其中包含" GPS设置和"取消"按钮将出现。如果用户选择" GPS设置"并启用GPS,MainActivity显示屏上的按钮" GPS开启"而不是" GPS已关闭。请将其打开"。如果启用GPS并启动应用程序,它将显示" GPS已打开" 。现在,在应用程序中,如果用户决定启用或禁用GPS,他只需单击显示GPS状态的按钮,它就会将他重定向到GPS设置,当他启用或禁用GPS并返回时,按钮必须显示目前的GPS状态。


现在唯一困扰我的部分是当GPS关闭并且应用程序启动并且用户通过警报框启用它时,GPS状态按钮不会改变,其工作正常。请指导我

GPSState = (Button) findViewById(R.id.bGPSstate);
        boolean isGPSEnabled = false;
// Declaring a Location Manager
        LocationManager locationManager;

        locationManager = (LocationManager) getApplicationContext()
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!isGPSEnabled) {
            // no GPS provider is enabled
            // displaying GPS status on the button and and opening GPS activity
            GPSState.setText("GPS is off.turn it on");
            GPSState.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    startActivityForResult(
                            new Intent(
                                    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS),
                            REQUESTCODE);

                }
            });
// creating alertdialog
            AlertDialog.Builder builder = new AlertDialog.Builder(c);

            builder.setTitle("Settings");
            builder.setMessage("Enable GPS for the Application");

            builder.setPositiveButton("GPS Setting",

            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    startActivity(new Intent(
                            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));

                    dialog.dismiss();
                }

            });

            builder.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            new AlertDialog.Builder(MainActivity.this)
                                    .setTitle("How to use Application")
                                    .setMessage(
                                            "You must enable the GPS in order to use this application. Press Activate and then press Power Button twice in order to send the alert message to the selected contacts")
                                    .setNeutralButton(
                                            "OK",
                                            new DialogInterface.OnClickListener() {

                                                @Override
                                                public void onClick(
                                                        DialogInterface dialog,
                                                        int which) {
                                                    // do something // for
                                                    // returning back to //
                                                    // application
                                                    dialog.cancel();

                                                }
                                            }).show();
                            dialog.dismiss();

                        }
                    });

            builder.show();

        } else {

        }
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        boolean isGPSEnabled = false;
        GPSState = (Button) findViewById(R.id.bGPSstate);
        LocationManager locationManager;
        locationManager = (LocationManager) getApplicationContext()
                .getSystemService(LOCATION_SERVICE);
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (!isGPSEnabled)
            GPSState.setText("GPS is off.Turn it on");
        else
            GPSState.setText("GPS is on");

1 个答案:

答案 0 :(得分:1)

你应该在活动的onResume()中检查gps状态,并在那里更新你的按钮。