【问题标题】:Difficulty in replying back(location of second user) to a first user's message ? [duplicate]难以回复(第二个用户的位置)第一个用户的消息? [复制]
【发布时间】: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


    【解决方案1】:

    问题可能是getLastKnownLocation() 如果不知道一个好的位置,它可以返回 null,如果没有指定位置,模拟器也会返回 null。如果你不想要当前位置,你必须准备等待并注册一个监听器:

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

    【讨论】:

    • 它仍然无法正常工作。还有什么建议吗?在我按照您的建议进行更改后,它将如下所示:这是链接:pastebin.com/XU9JTuiZ
    • 如果我没看错你在等待位置之前发送短信。您可能应该仅在调用侦听器后(在 onLocationChanged 内)发送回复,在此之前您可能没有位置。
    • 您能否通过指出需要重新排列的行来解释我在做什么,因为我认为我只是在调用 locationupdate() 后才发送消息,如果我错了,请纠正我?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多