难以回复第一个用户的消息(第二个用户的位置)?

发布于 2024-12-12 09:46:47 字数 3162 浏览 0 评论 0原文

可能的重复:
将用户 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();


    }

}

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度℉ 2024-12-19 09:46:48

问题可能在于,如果 getLastKnownLocation() 不知道合适的位置,它可能会返回 null,并且如果未指定位置,模拟器也会返回 null。如果您不想要当前位置,则必须准备等待并注册侦听器:

locationManager.requestLocationUpdates(LocationManager.GPS, 20000, 1, yourLocationListener);

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:

locationManager.requestLocationUpdates(LocationManager.GPS, 20000, 1, yourLocationListener);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文