我想在一个字符串中将latitud,Longitud存储在gps中,这样我就可以使用开放套接字将每个更改位置发送到我的新位置!所以任何帮助,以节省这个纬度和纵向!这是我的代码
package letget.h;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
public class letget extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " +
"Latitud ="+ loc.getLatitude() +
"Longitud =" + loc.getLongitude();
Toast.makeText( getApplicationContext(),
Text,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Disabled",
Toast.LENGTH_SHORT ).show();
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Enabled",
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}/* End of Class MyLocationListener */
}/* End of UseGps Activity*/
答案 0 :(得分:1)
你需要
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
在您的AndroidManifest.xml中访问GPS位置。
获取字符串的代码对我来说是正确的。要将其发送到套接字,您需要添加
import java.net.*
然后,假设您的python服务器正在侦听端口上的命令,请执行类似
的操作String cmd("[THE COMMAND TO SEND]");
InetSocketAddress address = new InetSocketAddress("[YOUR_URL]", [YOUR_PORT]);
DatagramPacket request = new DatagramPacket(cmd.getBytes(), cmd.length(), address);
DatagramSocket socket = new DatagramSocket();
socket.send(request);
(捕获Socket和IO异常)。发送命令显然取决于另一端的python脚本正在侦听的内容。您还可以使用类似HTTPConnection的内容并将值发布到它。
答案 1 :(得分:0)
您还需要将loc.getLatitude从double转换为字符串。 Double.toString(loc.getLatitude());