可能重复:
将用户1的位置发送给用户2和用户2';用户1的位置?
我有一个应用程序,其中有两个用户。用户1向用户2发送一些包含其位置的消息,作为回报,用户2向用户1发送一条包含其位置信息的消息。我可以将消息从用户1发送给用户2和受害者。用户1的消息按照应用程序中的预期包含其位置,但用户2的消息不是包含其位置而是包含用户1的位置,这与应用程序的预期相反。有人能告诉我哪里做错了吗?
这是我的代码:
public class ReceivelocationActivity extends BroadcastReceiver {
private static final String TAG = "LocationActivity";
private static final String LOCATION_SERVICE = null;
LocationManager locationManager;
Geocoder geocoder;
double longitude,latitude;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent m=new Intent(context, ReceivelocationActivity.class);
PendingIntent pi=PendingIntent.getBroadcast(context, 0, m, 0);
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
String str2="";
String str3="";
String autoReplyToken = "Request_Accepted";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " msgs[i].getOriginatingAddress();
str2=msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str3=msgs[i].getMessageBody().toString();
str += "n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
// int number=Integer.parseInt(str2);
SmsManager sms = SmsManager.getDefault();
boolean isAutoReply = str3.startsWith(autoReplyToken);
locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
geocoder = new Geocoder(this);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
Log.d(TAG, location.toString());
this.onLocationChanged(location);
}
String msg = Double.toString(latitude) + " " +Double.toString(longitude) ;
if (!isAutoReply) {
String autoReplyText = autoReplyToken + msg;
sms.sendTextMessage(str2, null, autoReplyText, pi, null);
}
// sms.sendTextMessage(str2, null, "Whats up", pi, null);
}
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
latitude=location.getLatitude();
longitude=location.getLongitude();
}
}
如果getLastKnownLocation()
不知道一个好的位置,它可能会返回null,如果没有指定位置,模拟器也会返回null。如果你不是目前的职位,你必须准备等待并注册一个监听器:
locationManager.requestLocationUpdates(LocationManager.GPS, 20000, 1, yourLocationListener);