如何让我的位置信息类通过短信发送坐标?

时间:2019-03-30 06:02:35

标签: java android gps sms

我在通过短信发送位置坐标时遇到困难。我无法正确构造发送短信。这是我的最高代码:

private static final int REQUEST_CODE = 1000;

FusedLocationProviderClient fusedLocationProviderClient;
LocationRequest locationRequest;
LocationCallback locationCallback;

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode)
    {
        case REQUEST_CODE:
        {
            if(grantResults.length > 0)
            {
                if(grantResults[0] == PackageManager.PERMISSION_GRANTED)
                {

                }
                else if(grantResults[0] == PackageManager.PERMISSION_DENIED)
                {

                }
            }
        }
    }
}

在我的onCreate中,双&&符号读取意外的令牌错误。我还认为我需要在此处添加一个私人空格,但不知道哪种类型与发送短信相对应。

if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION))
    {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
    }
    else
    {

        buildLocationRequest();
        buildLocationCallback();
        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

        {
        if (ActivityCompat.checkSelfPermission(FullscreenActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
            && ActivityCompat.checkSelfPermission(FullscreenActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION);
            ActivityCompat.requestPermissions(FullscreenActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
        return;
    }
    fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());

    }

    FusedLocationProviderClient provider = fusedLocationProviderClient;


    Location currentLocation = new Location(String.valueOf(provider));
    location("MY_PHONE_NUMBER", currentLocation);

在onCreate节之后,我最擅长的地方就是位置currentLocationString处的location方法,该位置说从未使用过参数,并且在getLongitude关闭时也遇到了错误。它包含分号错误,并且我没有在其中添加任何内容,例如)。我不知道我是否在正确地完成此位置/ SMS课程。

private void location(String phoneNumber, Location currentLocation) {
    SmsManager smsManager = SmsManager.getDefault();
    String smsBody = "maps.google.com?q=";
    smsManager.sendTextMessage(phoneNumber, null, smsBody, null, null);
}

private void buildLocationCallback() {
    locationCallback = new LocationCallback()
    {

        @Override
        public void onLocationResult(LocationResult locationResult) {
            for(Location location:locationResult.getLocations())
                SmsManager.getDefault(String.valueOf(location.getLatitude())
                        +","
                        +String.valueOf(location.getLongitude());
        }
    };
}

private void buildLocationRequest() {
    locationRequest = new LocationRequest();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(100);
    locationRequest.setFastestInterval(50);
    locationRequest.setSmallestDisplacement(10);
}

1 个答案:

答案 0 :(得分:0)

在buildLocationCallback之后添加getLastLocation()方法

git push --force

更新您的定位方法

private void getLastLocation() {
    try {
        fusedLocationProviderClient.getLastLocation()
                .addOnCompleteListener(new OnCompleteListener<Location>() {
                    @Override
                    public void onComplete(@NonNull Task<Location> task) {
                        if (task.isSuccessful() && task.getResult() != null) {
                            mLocation = task.getResult();
                            location("MY_PHONE_NUMBER", task.getResult());
                        } else {
                            Log.w(TAG, "Failed to get location.");
                        }
                    }
                });
    } catch (SecurityException unlikely) {
        Log.e(TAG, "Lost location permission." + unlikely);
    }
}