【问题标题】:Android studio Project Using LocationListener and LocationManager doesn't seem to run使用 LocationListener 和 LocationManager 的 Android Studio 项目似乎没有运行
【发布时间】:2016-12-06 02:40:30
【问题描述】:

我编写并编译了下面的程序并运行了它,但预期的目的是向textView 显示速度,据我在手机上运行它可以看出它不起作用。 有两个位置输出变量,speedourSpe,因为 ourSpe 来自我观看的 youtube 视频,但它不起作用,speed 来自我查找的堆栈溢出问题。两者都有帮助,但都没有打印出我按下按钮spedButt 的结果。我想我只是按错误的顺序编写了代码,但我也不确定我是否使用LocationManager 对。

布局文件在Relativelayout 中只有两个textViews 和一个Button,但堆栈溢出一直给我一个错误,我也想不出那个。我今天过得很糟糕。

主代码

    package com.example.vitaliy_2.safespeedalert;

    import android.Manifest;
    import android.content.Context;
    import android.content.pm.PackageManager;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.support.v4.app.ActivityCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;



    public class SpeedTest extends AppCompatActivity implements LocationListener {
            TextView txt;
            TextView txt_2;
            Button spedButt;
            float curSpe;
            float speed;
            Location l;
            Location mLastLocation;
            Location pCurrentLocation;

    @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_speed_test);



         txt = (TextView) findViewById(R.id.speed_display);
            txt_2 = (TextView) findViewById(R.id.speed_display_2);
            spedButt = (Button) findViewById(R.id.spedButt);
            speed = 0;

        LocationManager lm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

            if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=PackageManager.PERMISSION_GRANTED) {return;}
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
            this.onLocationChanged(null);

        }



        @Override
        public void onLocationChanged(final Location location) {
            l = location;
            if (this.mLastLocation != null)
                speed = (float )Math.sqrt(
                        Math.pow(pCurrentLocation.getLongitude() - mLastLocation.getLongitude(), 2)
                                + Math.pow(pCurrentLocation.getLatitude() - mLastLocation.getLatitude(), 2)
                ) / (pCurrentLocation.getTime() - this.mLastLocation.getTime());
            this.mLastLocation = pCurrentLocation;



            spedButt.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    if(l == null){
                        txt.setText("-.- m/s");
                        txt_2.setText("-.- m/s");

                    }else{
                        if (pCurrentLocation.hasSpeed())


   speed = pCurrentLocation.getSpeed();
                    curSpe = location.getSpeed();

                    String sent = speed + "m/s";
                    txt.setText(sent);
                    String sentTwo = curSpe + "m/s";
                    txt_2.setText(sentTwo);
                }
            }
        });

    }

【问题讨论】:

  • 您是否请求了位置权限?? developer.android.com/training/permissions/requesting.html
  • 是的@Budius。我在我发布的代码和清单中。我确实觉得很奇怪,对话框从来没有弹出,但程序从来没有给我一个运行时错误。
  • 你发布的代码只检查你是否有权限(r 显然你没有)当 IF 失败时你必须请求它。我有点没时间在这里为你写信了,但是再检查一下文档,它肯定会丢失它
  • 谢谢@Budius,我想通了。我认为自填的代码就足够了。
  • 因为在您接受权限后,您必须请求位置更新。当您成功请求时,它应该会出现指示器。你必须@Override onRequestPermissionsResult 方法。再次检查文档,其中解释了如何操作。

标签: java android locationmanager locationlistener


【解决方案1】:

回答我自己的问题,此代码会自动弹出,但仍然需要一个请求许可的子句。

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {


        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);


        return;
    }else{
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, this);
    }
    this.onLocationChanged(null);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 2021-11-27
    • 1970-01-01
    • 2022-12-04
    • 2022-06-10
    • 2016-05-23
    • 1970-01-01
    相关资源
    最近更新 更多