难以回复第一个用户的消息(第二个用户的位置)?
我有一个应用程序,其中有两个用户。用户 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();
}
}
Possible Duplicate:
Difficulty in sending location of user 1 to user 2 and user 2's location to user 1?
I have an application in which there are two users.User 1 sends some message containing his location to user 2 and in return user 2 sends a message conatining his location to user 1.I'm abled to send the message from user 1 to user 2 and vice-versa.User 1's message conatins his location as expected in application but user 2's message instead of conating his location,conatins the location of user 1,contrary to what expected by application.Can anyone tell me where I'm doing wrong?
Here is my code:
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题可能在于,如果
getLastKnownLocation()
不知道合适的位置,它可能会返回 null,并且如果未指定位置,模拟器也会返回 null。如果您不想要当前位置,则必须准备等待并注册侦听器:Could the problem be that
getLastKnownLocation()
can return null if it doesn't know a good location and emulator returns null too if location is not specified. If you wan't the current position you have to prepared to wait and register a listener: