简体   繁体   English

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

[英]How to get GPS location and send in SMS android

I can not get the GPS to work.我无法让 GPS 工作。 I am saving it in a SharedPreference called gps and it will print in a Toast as " http://maps.google.com/?q= " + lat + " , " + lon this is so when it is sent in a SMS they receiver can just push the link and it opens the map.我将它保存在一个名为 gps 的 SharedPreference 中,它将在 Toast 中打印为“ http://maps.google.com/?q= ” + lat + “ , ” + lon 当它通过 SMS 发送时就是这样他们的接收者只需按下链接即可打开地图。 But when the toast comes up it is blank.但是当吐司上来时,它是空白的。 Below is my code.下面是我的代码。

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);



}

to find your location try to use GPSTracker.要找到您的位置,请尝试使用 GPSTracker。 link 关联

And use this code.并使用此代码。

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

And one more things, check your device, is it support GPS?还有一件事,检查您的设备,它是否支持 GPS? And is it (GPS) enable?是否启用(GPS)? If your device's GPS is not enable, you will get null....如果您设备的 GPS 未启用,您将获得 null....

For more help... check my github repo如需更多帮助...请查看我的 github 存储

I am trying to make such app , here are the code for toast that works well:我正在尝试制作这样的应用程序,这里是运行良好的吐司代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM