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

Difficulty in sending location of user 1 to user 2 and user 2#39;s location to user 1?(很难将用户 1 的位置发送给用户 2,将用户 2 的位置发送给用户 1?)

本文介绍了很难将用户 1 的位置发送给用户 2,将用户 2 的位置发送给用户 1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a code which sends location of user 1 to user 2 and user 2's location to user 1 . The location of user 1 is send perfectly to user 2 and user 2 is even sending a message back to user 1 but the location which it is sending is the location of user 1 not his (user 2) location.

这是我的代码:

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 += "
";        
            }
            //---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 );
    }
}

谁能告诉我哪里做错了?

Can anyone tell me where am I doing wrong?

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

pastebin.com/53ZJH3iN this is the file which reply back to a sms received from user 1 . Instead of sending user 2's location it is sending same location obtained from user 1 back to him.(Here location refers to longitude and latitude). Please help me.I'm breaking my head on this

推荐答案

你遇到的一个问题是在第 102 行:

Well one problem you have is at line 102:

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

您尚未更新纬度和经度以反映您当前的位置.在该行之前添加此代码:

You haven't updated latitude and longitude to reflect your current position. Add this code right before that line:

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

看看修复后会发生什么.

See what happens once that is fixed.

这篇关于很难将用户 1 的位置发送给用户 2,将用户 2 的位置发送给用户 1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:很难将用户 1 的位置发送给用户 2,将用户 2 的位置发送给用户 1?