2011-10-29 3 views
-3

可能性の重複:
Difficulty in sending location of user 1 to user 2 and user 2's location to user 1?最初のユーザーのメッセージに返信(2番目のユーザーの場所)が難しいですか?

Iは、2つのusers.User 1が存在するユーザ2に彼の位置を含む、いくつかのメッセージを送信し、戻りユーザ2にメッセージを送信するアプリケーションを持っています私は自分の位置をユーザ1に結びつける。私は、ユーザ1からユーザ2に、そしてその逆にメッセージを送信することはできない。ユーザ1のメッセージは、アプリケーションでは期待通りに彼の位置をconatinするが、ユーザー1は、application.Canの誰かが私がどこにいるか教えてくれるのとは反対に間違っている?ここで

は私のコードです:

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


    } 

} 

答えて

0

問題は、それは場所が指定されていない場合はリターンがあまりにもヌル良い場所とエミュレータを知らない場合getLastKnownLocation()はnullを返すことができるということでした。あなたが現在のポジションを望んでいない場合は、待機してリスナーを登録する準備が必要です。

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

これはまだ動作していません。他の提案はありますか?あなたが提案したように変更を加えた後、次のようになります:http://pastebin.com/XU9JTuiZ –

+0

私が正しく見たなら、あなたはその場所を待つ前にSMSを送ってください。リスナーが(onLocationChangedの中で)呼び出された後にのみ返信を送信することをお勧めします。 – Lycha

+0

私はlocationupdate()を呼び出した後にのみメッセージを送信していると思いますので、私が間違っている場合は修正してください。 –

関連する問題