获取经度和纬度在模拟器中工作但不在设备上工作?

时间:2013-07-03 22:38:21

标签: android gps emulation

我一直在尝试使用GPS坐标,但它比我想象的要困难得多。经过几个小时的试验和错误,我已经设法输出我的模拟器的纬度和经度(模拟)。以下是我完成的两种方式:

第一种方式:

import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String text = "";

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if(location != null) {
            showMyAddress(location);
        }

        final LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                showMyAddress(location);
            }
            public void onProviderDisabled(String arg0) {
            }
            public void onProviderEnabled(String arg0) {
            }
            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            }
        };

        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 10, locationListener);

        double longitude = location.getLongitude();
        double latitude = location.getLatitude();

        Log.i(TAG, "longitude: " + longitude);
        Log.i(TAG, "latitude: " + latitude);

        text = "longitude: " + longitude + ", " + "latitude: " + latitude;

        TextView textView = (TextView) findViewById(R.id.txtvwMain);
        textView.setText(text);
    }

    private void showMyAddress(Location location) {
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());   
        List<Address> myList;
        try {
            myList = myLocation.getFromLocation(latitude, longitude, 1);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

第二种方式:

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.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity implements LocationListener
{
    private LocationManager lm;
    private TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.txtvwMain);
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, this);

        Location location = lm.getLastKnownLocation(lm.GPS_PROVIDER);
        double longitude = location.getLongitude();
        double latitude = location.getLatitude();
        tv.setText("latitude="+latitude+", longitude="+longitude);
    }

    @Override
    public void onLocationChanged(Location arg0) {
        String lat = String.valueOf(arg0.getLatitude());
        String lon = String.valueOf(arg0.getLongitude());
        Log.e("GPS", "location changed: lat="+lat+", lon="+lon);
        tv.setText("lat="+lat+", lon="+lon);
    }
    public void onProviderDisabled(String arg0) {
        Log.e("GPS", "provider disabled " + arg0);
    }
    public void onProviderEnabled(String arg0) {
        Log.e("GPS", "provider enabled " + arg0);
    }
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        Log.e("GPS", "status changed to " + arg0 + " [" + arg1 + "]");
    }
}

上述两种方式都可以将模拟纬度和经度打印到模拟器中的TextView上(运行Android 4.2.2)。但是,当我将.apk文件上传到我的设备(运行Android 4.0.4的平板电脑)时,它只会崩溃。我不知道什么是错的,因为我看不到任何错误消息。问题的根源是什么,我该如何解决?感谢。

0 个答案:

没有答案
相关问题