如何获取GPS位置并发送短信android

时间:2015-05-07 03:20:12

标签: android gps sms

我无法让GPS工作。我将它保存在名为gps的SharedPreference中,它将以Toast打印为" http://maps.google.com/?q=" + lat +" ," + lon 这是当它通过短信发送时,接收者可以只是推送链接,它打开地图。但是当吐司出现时,它是空白的。以下是我的代码。

private void showLocation() {

    LocationManager locationManager = (LocationManager)
            getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(bestProvider);
    LocationListener loc_listener = new LocationListener() {

        public void onLocationChanged(Location l) {}

        public void onProviderEnabled(String p) {}

        public void onProviderDisabled(String p) {}

        public void onStatusChanged(String p, int status, Bundle extras) {}
    };
    locationManager
            .requestLocationUpdates(bestProvider, 0, 0, loc_listener);
    location = locationManager.getLastKnownLocation(bestProvider);
    double lat, lon;
    try {
        lat = location.getLatitude();
        lon = location.getLongitude();
    } catch (NullPointerException e) {
        lat = -1.0;
        lon = -1.0;
    }

          prefs.setPreferenceString(getApplicationContext(),"gps" ,"http://maps.google.com/?q=" + lat +  " , " + lon);



}

2 个答案:

答案 0 :(得分:0)

找到您的位置尝试使用GPSTracker。 link

并使用此代码。

        gps = new GPSTracker(LauncherActivity.this);
        latitude = gps.getLatitude();
        longitude = gps.getLongitude();

还有一件事,检查你的设备,是否支持GPS?是(GPS)启用?如果您的设备未启用GPS,则会显示空值....

如需更多帮助...请查看我的github repo

答案 1 :(得分:0)

I am trying to make such app,下面是运行良好的吐司代码:

  Handler handler3 = new Handler(Looper.getMainLooper());
    handler3.post(new Runnable(){
        @Override
        public void run(){
            Toast.makeText(context,"mensagem",Toast.LENGTH_LONG).show();
        }
    });