我正在开发一个获取纬度和经度的Android应用程序,并将其发送给有短信的人。我遇到的问题是对方没有收到我的短信。你能看看发生了什么事吗?我从今天早上起就开始工作了,它让我吃香蕉。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button sendSMS;
TextView txtLocation;
Context context;
double lat, lng;
LocationManager locationManager;
Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendSMS = (Button) findViewById(R.id.button_send_sms);
txtLocation = (TextView) findViewById(R.id.txt_location);
sendSMS.setOnClickListener(this);
context = getApplicationContext();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);
String provider = locationManager.getBestProvider(criteria, true);
PackageManager packageManager = getPackageManager();
if (packageManager.checkPermission(android.Manifest.permission.ACCESS_FINE_LOCATION, getPackageName()) == PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(provider, 10000, 100, locationListener);
location = locationManager.getLastKnownLocation(provider);
getCoordinates(location);
}
}
private void getCoordinates(Location l){
if (l != null) {
lat = l.getLatitude();
lng = l.getLongitude();
txtLocation.setText(Latitude: + " " + lat + "\n" + Longitude + " " + lng);
}
else{
txtLocation.setText("No location found yet");
}
}
@Override
public void onClick(View v) {
Intent intent;
switch (v.getId()){
case R.id.button_send_sms:
String number;
String msgbody;
number = "0123456789";
if(lat!=0 && lng!=0) {
msgbody = "My location is: http://google.com/maps/place/" + lat + "," + lng;
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, msgbody, null, null);
Toast.makeText("Message sent"), Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText("Message didn't send"), Toast.LENGTH_SHORT).show();
}
break;
}
}
private final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
getCoordinates(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
}
我在手机屏幕上看到的结果是Toast“Message sent”但我看不到另一部手机收到的短信,我的logcat没有错误。当我按下按钮时,logcat写道:
12-07 00:15:54.494 28690-28690/emmanouilvaresis.findtheold D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
AndroidManifest还拥有短信的所有权限。
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
你能告诉我为什么不发送短信吗?我做错了什么?