Java 如何让我的位置类通过短信发送坐标?
我很难通过短信发送我的位置坐标。我无法正确设置发送短信的帧。以下是我的最高代码:Java 如何让我的位置类通过短信发送坐标?,java,android,gps,sms,Java,Android,Gps,Sms,我很难通过短信发送我的位置坐标。我无法正确设置发送短信的帧。以下是我的最高代码: private static final int REQUEST_CODE = 1000; FusedLocationProviderClient fusedLocationProviderClient; LocationRequest locationRequest; LocationCallback locationCallback; @Override public void onRequestPermi
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中,double&&符号读取了一个意外的令牌错误。我还认为我需要在那里添加一个私人空白,但不知道哪种对应发送短信
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部分之后,我遇到了最大的困难,尤其是location currentLocation字符串中的location方法,它表示从未使用过参数,并且我在getLongitude关闭时也遇到了一个错误。它包含一个带有分号的错误,我在那里添加的任何内容(如a)都无效。我不知道我是否正确地上了这个位置/短信课程
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);
}
在buildLocationCallback之后添加getLastLocation()方法
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);
}
}
private void location(String phoneNumber, Location currentLocation) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, "maps.google.com?q=" + currentLocation.getLatitude() + "," + currentLocation.getLongitude(), null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}