【发布时间】:2011-10-29 06:54:50
【问题描述】:
可能重复:
Difficulty in sending location of user 1 to user 2 and user 2's location to user 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();
}
}
【问题讨论】:
标签: java android gps sms location