我正在为学校制作应用程序..我的应用程序功能专门用于通过短信向用户发送最准确的位置。所以我使用融合位置api ...但现在我不知道如何将SMS管理器整合到我当前的应用程序中......目前我只构建了一个简单的应用程序,通过按下按钮显示用户的当前位置 我只想获得有关如何在我的应用程序中合并SMS管理器的帮助。 我还希望消息以URL形式加上坐标,以便接收者可以轻松跟踪用户。
这是我的代码主要活动
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);
}
}
非常感谢您的帮助。
答案 0 :(得分:0)
以下是一个示例,将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);
以上链接应将收件人重定向到Google地图。
不要忘记在清单中添加此权限:
<uses-permission android:name="android.permission.SEND_SMS"/>