难以将用户 1 的位置发送给用户 2,将用户 2 的位置发送给用户 1?



我有一个代码,将用户1的位置发送给用户2,并将用户2的位置发送给用户1。用户1的位置被完美地发送给用户2,用户2甚至向用户1发送消息,但它发送的位置是用户1的位置,而不是他(用户2)的位置。

下面是我的代码:
package com.example.gui;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class ReceivelocationActivity extends BroadcastReceiver  {
    private static final String TAG = "LocationActivity";
    public static final String SMS_URI = "content://sms";
    public static final String ADDRESS = "address";
    public static final String PERSON = "person";
    public static final String DATE = "date";
    public static final String READ = "read";
    public static final String STATUS = "status";
    public static final String TYPE = "type";
    public static final String BODY = "body";
    public static final String SEEN = "seen";

    public static final int MESSAGE_TYPE_INBOX = 1;
    public static final int MESSAGE_TYPE_SENT = 2;
    public static final int MESSAGE_IS_NOT_READ = 0;
    public static final int MESSAGE_IS_READ = 1;
    public static final int MESSAGE_IS_NOT_SEEN = 0;
    public static final int MESSAGE_IS_SEEN = 1;
    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();

    }
    private void putSmsToDatabase( ContentResolver contentResolver, SmsMessage sms )
    {
        // Create SMS row
        ContentValues values = new ContentValues();
        values.put( ADDRESS, sms.getOriginatingAddress() );
        values.put( DATE, sms.getTimestampMillis() );
        values.put( READ, MESSAGE_IS_NOT_READ );
        values.put( STATUS, sms.getStatus() );
        values.put( TYPE, MESSAGE_TYPE_INBOX );
        values.put( SEEN, MESSAGE_IS_NOT_SEEN );

        // Push row into the SMS table
        contentResolver.insert( Uri.parse( SMS_URI ), values );
    }
}

谁能告诉我我哪里做错了?

pastebin.com/53ZJH3iN这是回复从用户1收到的短信的文件。它不是发送用户2的位置,而是将从用户1获得的相同位置发送回用户1。(这里的位置是指经度和纬度)。请帮帮我。

第102行有一个问题:

String msg = Double.toString(latitude) + " " +Double.toString(longitude) ;

您没有更新经纬度来反映您当前的位置。在该行之前添加以下代码:

latitude = location.getLatitude();
longitude = location.getLongitude();

查看修复后会发生什么

最新更新