难度在发送用户1位置,用户2和用户2的位置,用户1? [英] Difficulty in sending location of user 1 to user 2 and user 2's location to user 1?

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

问题描述

我有一个code,它发送的用户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.

下面是我的code:

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 );
    }
}

谁能告诉我,我做错了?

Can anyone tell me where am I doing wrong?

pastebin.com/53ZJH3iN这是回复给用户,从1接收短信的文件。相反,发送用户2的位置,它是把从用户1回他获得同样的位置。(这里的位置指的经度和纬度)。请帮助me.I'm打破我的头在这个

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) ;

您还没有更新的经度和纬度,以反映您的当前位置。该行之前添加此code正确的:

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆