如何在Android中通过短信发送位置坐标?

时间:2018-10-27 09:41:02

标签: android

有人可以指导我如何通过短信将手机的经度和纬度发送到另一个电话号码吗?我以哪种格式输入本地或国际电话号码。

这是我的代码,清单中已经有权限:

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.widget.EditText;

public class NewActivity extends AppCompatActivity {

    static final int REQUEST_LOCATION = 1;
    LocationManager locationManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from new_activity.xml
        setContentView(R.layout.new_activity);
        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        getLocation();
    }
    void getLocation() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {

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

        } else {
            Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            if (location != null) {
                double latti = location.getLatitude();
                double longi = location.getLongitude();

                ((EditText) findViewById(R.id.Lati)).setText("Latitude: " + latti);
                ((EditText) findViewById(R.id.Longi)).setText("Longitude: " + longi);
                try {
                    SmsManager manager = SmsManager.getDefault();
                    String message;
                    String telNumber;
                    telNumber = "XXXXXXXXXX";
                    // Get the string
                    message = "[AUTOMATIC MESSAGE]\n" + "I am currently at \n"+"Latitude" + latti +
                            "and \n"+"Longitude" + longi + " (approximately; accuracy within 100 meters).";
                    manager.sendTextMessage(telNumber, null, message, null, null);
                } catch (Exception ignored) {

                }
            }else {
                ((EditText) findViewById(R.id.Lati)).setText("Unable to find correct location.");
                ((EditText) findViewById(R.id.longi)).setText("Unable to find correct location. ");
            }
        }
    }

        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,@NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);

            switch (requestCode) {
                case REQUEST_LOCATION:
                    getLocation();
                    break;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

电话号码必须为

  

电话:电话号码