2013-04-29 49 views
1

開發一款徒步應用程序,只需點擊應用程序中的按鈕即可發送人員經緯度座標。當我觸發發送按鈕時,該消息只發送「發送緊急服務到:」nullnull「,意味着它沒有保存lat和long的值與消息。我有完整的gps信號並在清單中具有以下權限。如何在Android中存儲GPS lat和long發送短信?

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> 

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 

<uses-permission android:name="android.permission.INTERNET" /> 

<uses-permission android:name="android.permission.TELEPHONY_MANAGER" /> 

<uses-permission android:name="android.permission.SEND_SMS" /> 

我需要什麼,我下面的代碼更改爲使應用程序成功發送的緯度和經度座標與文本消息。 道歉所有的「吐司」的消息,將它們用於測試目的!

package com.gpscamera; 

import java.util.ArrayList; 

import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.telephony.SmsManager; 

public class MainActivity extends Activity { 
Button sendButton; 
ImageView iv; /* result of the data returned in the if statement is 
       INCLUDED IN THE USER INTERFACE*/ 

TextView textLat; 
TextView textLong; 
String long1; 
String lat1; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    textLat = (TextView)findViewById(R.id.textLat); 
    textLong = (TextView)findViewById(R.id.textLong); 


    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    LocationListener ll = new mylocationlistener(); 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); 

    iv=(ImageView) findViewById(R.id.imageView); 
    /* Reference to out imageview view */ 
    Button btn = (Button) findViewById(R.id.takePhoto); 
    /* Makes reference to button to start the application, button name is takePhoto */ 
    btn.setOnClickListener(new OnClickListener() { 
     /* Makes the onclick listener for the button, */ 

     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      /* in this onclikc, must define what happens when the button is clicked. */ 
      /* Specified to open the image caputre*/ 
      startActivityForResult(intent, 0); 
      /* Starts that activity "intent" above */ 


       } 
    }); 
    Button btn1 = (Button) findViewById(R.id.Send); 
    btn1.setOnClickListener(new OnClickListener() { 
     /* Makes the onclick listener for the button, */ 

     @Override 
     public void onClick(View v) { 

      /* Starts that activity "intent" above */ 
      Toast.makeText(getApplicationContext(), "eeeeeeeee", Toast.LENGTH_SHORT).show(); 
      String phoneNumber = "07835588000"; 
      String message = "Send Emergency Services to:"; 

      SmsManager smsManager = SmsManager.getDefault(); 
      ArrayList<String> parts = smsManager.divideMessage(message); 
      smsManager.sendMultipartTextMessage(phoneNumber, null, parts, null, null); 
      Toast.makeText(getApplicationContext(), "after sms", Toast.LENGTH_SHORT).show(); 

       } 
    }); 
} 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
/* When the button is clicked it is going to start the camera, returning a result which is an image 
* with the request code from the intent above. */ 

{ 
    if(requestCode ==0) 
    /* If the request code is = to 0 we need to get the bitmap image which is returned as out data. */ 
    { 
     Bitmap theImage = (Bitmap) data.getExtras().get("data"); 
     iv.setImageBitmap(theImage); 
     Toast.makeText(getApplicationContext(), "photo saved", Toast.LENGTH_SHORT).show(); 
     Toast.makeText(getApplicationContext(), "Getting GPS loc", Toast.LENGTH_SHORT).show(); 
    } 
} 
class mylocationlistener implements LocationListener{ 

    @Override 
    public void onLocationChanged(Location Location) { 
     try { 

     if(Location != null) 
     { 
      double plong = Location.getLongitude(); 
      double plat = Location.getLatitude(); 

      textLat.setText(Double.toString(plat)); 
      textLong.setText(Double.toString(plong)); 

      textLat.append(lat1); 
      textLong.append(long1); 

     } 
     } catch (Exception ex) { 
      Toast.makeText(getApplicationContext(), "loss signal", Toast.LENGTH_SHORT).show(); 
     } 


    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, 
      Bundle extras) { 
     // TODO Auto-generated method stub 

    } 
} 

} 
+0

'textLat.append(lat1); textLong.append(long1);'它看起來好像是在實例化'lat1'或'long1'變量。此外,它看起來並不像實際上將經緯度值放入從SMS發送的字符串中。 – FoamyGuy 2013-04-29 16:18:56

回答

0

在我看來,當你在public void onLocationChanged(Location Location) {中獲得位置時,只需更新TextView,而不要在SMSManager中使用這些實際值。這裏:smsManager.sendMultipartTextMessage(phoneNumber, null, parts, null, null); 您使用的是持有「發送緊急服務到:」的部分,而不是位置的實際值。

0

您不更新用於發送短信的字符串中的您的位置詳細信息。嘗試下面的僞代碼。希望能幫助到你!!

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
locationText = "Current Location ="+location.getLatitude() + "," + location.getLongitude(); 
smsManager.sendTextMessage(phoneNo, null, locationText, null, null);