开发者

GPS on Android problem

开发者 https://www.devze.com 2023-04-06 19:32 出处:网络
I have been trying for about 3 days to solve this problem and I haven\'t found a solution so far. I am trying to get my location through GPS.

I have been trying for about 3 days to solve this problem and I haven't found a solution so far. I am trying to get my location through GPS. It doesn't seem to receive the coordonates that I try to send through cmd.exe or the Emulator Control in the DDMS. Moreover, it restarts the emulator. The code that I have been trying is below. If someone can help me then please do because I don't find any solution to this problem. Thank you!

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class Check_Location extends Activity {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

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

LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {

TextView tv=(TextView) findViewById(R.id.textView1);
tv.setText(location.getLongitude()+"  "+location.getLatitude());
}

public void onStatusChanged(String provider, int status, Bundle extras) {}

public void onProviderEnabled(String provider) {}

public void onProviderDisabled(String provider) {}
};

//Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,   locationListener);
}
}

Another method that I have tried and also didn't work is below:

  import android.app.Activity;
  import android.content.Context;
  import android.location.Criteria;
  import android.location.Location;
  import android.location.LocationListener;
  import android.location.LocationManager;
  import android.os.Bundle;
  import android.util.Log;
  import android.widget.TextView;
  import android.widget.Toast;

 public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

}

/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
    @Override
    public void onLocationChanged(Location loc)
    {
        loc.getLatitude();
        loc.getLongitude();

        String Text = "My current location is: " +
                      "Latitude = " + loc.getLatitude() +
                      "Longitude = " + loc.getLongitude();

        Toast.makeText( getApp开发者_运维知识库licationContext(), Text, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderDisabled(String provider)
    {
        Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
    }

    @Override
    public void onProviderEnabled(String provider)
    {
        Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
    }

}/* End of Class MyLocationListener */
}

The permisssions set for both are:

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


I managed to solve my problem. I have read on other forums. Both codes are working but try on an Android platform that is Less than level 10. For example make an emulator for level 8. It works!

0

精彩评论

暂无评论...
验证码 换一张
取 消