当前位置: 首页 > 面试题库 >

将用户1的位置发送给用户2以及将用户2的位置发送给用户1困难吗?

叶弘深
2023-03-14
问题内容

我有一个将用户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获得的相同位置发回给他。(这里的位置是指经度和纬度)。请帮助我。


问题答案:

好吧,您遇到的一个问题是第102行:

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

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

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

看看一旦解决,会发生什么。



 类似资料:
  • 问题内容: 我在磁盘上有一个pdf文件,当他们向php脚本发出请求时需要将其发送给用户,这样做的最佳方法是什么? 谢谢 问题答案: 假设它在服务器上: readfile()-输出文件

  • 我有一个与: 以及我的带有useReducer钩子的提供者上下文。以及一个useEffect钩子,用于调度操作“setCurrentUser”,并在客户端登录时将“isAuthenticated”设置为true。 我放了一个控制台。在我的组件中记录(stateUser.isAuthenticated),这将显示“false”(初始值),然后显示“true”(分派) 我需要将状态(stateUser

  • 我想使用贝宝来允许我的客户用信用卡或借记卡付款,我已经允许用贝宝付款,我知道在登录贝宝表单的按钮下,它的一个按钮上写着“用信用卡或借记卡付款”,但我想让用户直接从脚本到贝宝客人结账。 我使用PayPal SDK,这是我的代码: 我需要这个地方 而不是这个

  • 我相信,当当前用户访问经过身份验证的URL时,可以获取他们的信息,但是为每个登录用户存储所有接收器的方法是什么呢? 我很感激。

  • 我正在尝试在Netty中开始使用HTTP/2。我尝试了“hello world”示例,它按预期工作。在示例中,管道如下所示: 服务器: SslHandler 应用程序协议协商处理程序 Http2ConnectionHandler 客户: SslHandler 应用程序协议协商处理程序 HttpToHttp2ConnectionHandler

  • 问题内容: 我正在建立一个自助餐厅管理系统,其中有两种类型的用户;客户和柜台所有者。 对于自助餐厅中提供的每道菜,都有一个与之相关的独特的dish.id。 到现在为止,我有一个类似圆顶的东西,例如学生向带有URL的POST请求发送一个带有菜品,标识和计数器编号的POST请求,该URL更新“ orders table”(以PK作为ID)。准备好菜品后,柜台所有者按“已废除” ”按钮。 在这一点上,我