如何使用短信发送位置? (融合位置api)
How to send location using sms? (fused location api)
我目前正在为学校制作一个应用程序。我的应用程序专门用于通过短信向用户发送最准确的位置信息。所以我使用了 fused location api ...但现在我不知道如何将 SMS 管理器合并到我当前的应用程序中...目前我只构建了一个简单的应用程序,它通过按 a 显示用户当前位置按钮
我只需要有关如何将 SMS 管理器整合到我的应用程序中的帮助。
我还希望消息以 URL 形式加上坐标,以便接收者可以轻松跟踪用户。
这是我的主要代码 activity
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 2300;
TextView textView;
Button sosbtn;
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) {
}
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
sosbtn = (Button) findViewById(R.id.sosbtn);
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION)) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
} else {
buildLocationRequest();
buildLocationCallBack();
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
sosbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
return;
}
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
}
});
}
}
private void buildLocationCallBack() {
locationCallback= new LocationCallback()
{
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
for(Location location:locationResult.getLocations())
textView.setText(String.valueOf(location.getLatitude())+"/"+String.valueOf(location.getLongitude()));
}
};
}
private void buildLocationRequest() {
locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(5000);
locationRequest.setFastestInterval(3000);
locationRequest.setSmallestDisplacement(5);
}
}
非常感谢您的帮助。
这是一个示例,将 location
对象声明为私有字段并单击您的按钮:
SmsManager smsManager = SmsManager.getDefault ();
String message = "My location: https://www.google.com/maps/@?api=1&map_action=map¢er=" + location.getLatitude () + "," + location.getLongitude ();
String phoneNumber = "+1....";
smsManager.sendTextMessage (phoneNumber, null, message, null, null);
以上 link 应该将您的收件人重定向到 google 地图。
不要忘记在您的清单中添加此权限:
<uses-permission android:name="android.permission.SEND_SMS"/>
和 request the send sms permission 在运行时。
我目前正在为学校制作一个应用程序。我的应用程序专门用于通过短信向用户发送最准确的位置信息。所以我使用了 fused location api ...但现在我不知道如何将 SMS 管理器合并到我当前的应用程序中...目前我只构建了一个简单的应用程序,它通过按 a 显示用户当前位置按钮 我只需要有关如何将 SMS 管理器整合到我的应用程序中的帮助。 我还希望消息以 URL 形式加上坐标,以便接收者可以轻松跟踪用户。
这是我的主要代码 activity
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 2300;
TextView textView;
Button sosbtn;
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) {
}
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
sosbtn = (Button) findViewById(R.id.sosbtn);
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION)) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
} else {
buildLocationRequest();
buildLocationCallBack();
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
sosbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
return;
}
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
}
});
}
}
private void buildLocationCallBack() {
locationCallback= new LocationCallback()
{
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
for(Location location:locationResult.getLocations())
textView.setText(String.valueOf(location.getLatitude())+"/"+String.valueOf(location.getLongitude()));
}
};
}
private void buildLocationRequest() {
locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(5000);
locationRequest.setFastestInterval(3000);
locationRequest.setSmallestDisplacement(5);
}
}
非常感谢您的帮助。
这是一个示例,将 location
对象声明为私有字段并单击您的按钮:
SmsManager smsManager = SmsManager.getDefault ();
String message = "My location: https://www.google.com/maps/@?api=1&map_action=map¢er=" + location.getLatitude () + "," + location.getLongitude ();
String phoneNumber = "+1....";
smsManager.sendTextMessage (phoneNumber, null, message, null, null);
以上 link 应该将您的收件人重定向到 google 地图。
不要忘记在您的清单中添加此权限:
<uses-permission android:name="android.permission.SEND_SMS"/>
和 request the send sms permission 在运行时。