在Android

时间:2016-08-27 19:30:31

标签: android location

我正在开发一个迷你应用来检测我在本网站http://androidexample.com/GPS_Basic__-__Android_Example/index.php?view=article_discription&aid=68上关注的用户的当前位置。我能够在模拟器本身的扩展控件中更改Lat和Lon。但是当我在我的小米1S(运行版本4.2.2)手机中使用时它没有显示我的任何位置。以下是我的代码

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
    btnSpoof = (Button) findViewById(R.id.btn_spoof);
    lblShowLat = (TextView) findViewById(R.id.lblShowLat);
    lblShowLon = (TextView) findViewById(R.id.lblShowLon);
    lblShowTime = (TextView) findViewById(R.id.lblShowTime);

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



    btnShowLocation.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

        }


        });

    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                while (!isInterrupted()) {
                    Thread.sleep(1000);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                                updateDateTime();

                            // Toast.makeText(getApplicationContext(), "Refresh ", Toast.LENGTH_LONG).show();
                        }
                    });
                }
            } catch (InterruptedException e) {
            }
        }
    };

    t.start();



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

        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {

        } else {


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

        }
    }

    lblShowLat.setText("No Lat");
    lblShowLon.setText("No Lon");
}


@Override

//code does not run here

public void onLocationChanged(Location location) {

    if(location !=null) {

        lblShowLat.setText(Double.toString(location.getLatitude()));
        lblShowLon.setText(Double.toString(location.getLongitude()));
        String str = "Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude();
        System.out.println("Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude());
        Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();
    }
    else
    {
        Toast.makeText(this, "Unable to find your location, try again", Toast.LENGTH_SHORT).show();
    }

}

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

我想你忘了添加这一行,

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,10,这一点);

,以及确保设备gps和网络是否启用互联网连接。

答案 1 :(得分:0)

请添加这些行..这对你有用。

添加权限: -

android.permission.ACCESS_FINE_LOCATION

android.permission. ACCESS_COARSE_LOCATION

android.permission.INTERNET

在Java.class中: -

    @Override
    protected void onCreate(Bundle savedInstanceState)
     {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      txtLat = (TextView) findViewById(R.id.textview1);

   locationManager = (LocationManager)   getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,     this);
     }

   @Override
  public void onLocationChanged(Location location)
    {
    txtLat = (TextView) findViewById(R.id.textview1);
    txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" +       location.getLongitude());
     }

 @Override
 public void onProviderDisabled(String provider)
   {
   Log.d("Latitude","disable");
     }

 @Override
  public void onProviderEnabled(String provider) {
   Log.d("Latitude","enable");
    }

  @Override
   public void onStatusChanged(String provider, int status, Bundle extras) {
  Log.d("Latitude","status");
     }
   }