【问题标题】:Android Trouble with displaying GPS Status on the ButtonAndroid 在按钮上显示 GPS 状态时出现问题
【发布时间】:2014-02-12 09:06:33
【问题描述】:

在我的应用程序中,如果 GPS 已关闭且应用程序启动,则会出现一个警报对话框,其中包含“GPS 设置”和“取消”按钮,如果 GPS 已打开且应用程序将启动,则不会发生任何事情。在应用程序中,有一个显示 GPS 状态的按钮,即如果 GPS 开启,它将显示“GPS 开启”,如果关闭“GPS 关闭,打开它”,当用户单击它时,它将打开 GPS 设置当用户启用 GPS 并返回时,按钮必须显示“GPS 已开启”。当用户启用 GPS 时,我发现在按钮上显示状态时遇到问题。请帮忙。我是安卓新手。

      private Button GPSState;
        GPSState = (Button) findViewById(R.id.bGPSstate);
        boolean isGPSEnabled = false;
        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 Settings
                    GPSState.setText("GPS is OFF.Turn it On");
                    GPSState.setOnClickListener(new View.OnClickListener() {

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

                        }
                    });
        // 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 { // GPS provider is enabled }
                    GPSState.setText("GPS is On");

                }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);

GPSState.setText("GPS is on");
}

【问题讨论】:

    标签: android gps


    【解决方案1】:

    当您调用“设置”以启用 GPS 时,请使用 startActivityForResult 而不是 startActivity。用户在设置页面启用/禁用 GPS 后,他/她将按 返回 按钮返回您的应用。此时,方法onActivityResult 被调用。您可以在此处设置文本。

    如何设置文本?大致:

    onActivityResult() {
        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");
        else GPSState.setText("GPS is on");
    }
    

    目前我没有看到 android 文档或 eclipse。但我认为这可以给你一个线索。不过我知道方法不是这样的。

    【讨论】:

    • 我已经完成了 startActivityForResult,请告诉我如何设置文本 onActivityResult。
    • 非常感谢。它有帮助。实际上我对 onActivityResult 感到困惑。非常感谢
    【解决方案2】:

    使用此代码:

     public static boolean isGpS(Context mContext)
        {
             final LocationManager manager = (LocationManager)mContext.getSystemService( Context.LOCATION_SERVICE );
    
                if (!manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) 
                {
                    return false;
                }
                else
                {
                    return true;
                }
        }
    

    在你的 onCreate 中写下这个:

    if(!Utils.isGpS(Youractivity.this)
    {   
       buildAlertMessageNoGps();
    }
    

    这是 showAlertDialog 的方法:

    private void buildAlertMessageNoGps()
     {
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Yout GPS seems to be disabled, do you want to enable it?")
                   .setCancelable(false)
                   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                       public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                           startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                       }
                   })
                   .setNegativeButton("No", new DialogInterface.OnClickListener() {
                       public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                            dialog.cancel();
                       }
                   });
            final AlertDialog alert = builder.create();
            alert.show();
        }
    

    注意:不要忘记在 AndroidMenifest 文件中应用权限。

        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
        <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
    

    【讨论】:

      猜你喜欢
      • 2020-08-13
      • 2012-07-28
      • 1970-01-01
      • 1970-01-01
      • 2022-12-17
      • 1970-01-01
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多