我有一个应用程序,我希望通过电子邮件发送我的实际坐标。
打开电子邮件客户端的代码如下:
final Button button = (Button) findViewById(R.id.addbutton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Funktion aufrufen, aber ausserhalb definieren
Intent i = new Intent(Intent.ACTION_SENDTO);
//i.setType("message/rfc822");
i.setData(Uri.parse("mailto:"));
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"mymail@example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT , "body of email";
try {
startActivity(i);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MapsActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
});
button
这样的工作正常。
现在我认为我可以使用实际latitude
定义两个变量,使用当前longitude
定义另一个变量。
为此,我必须得到实际的坐标。
所以我的问题是:如何将当前的 GPS 坐标放入电子邮件中。
请:保持代码尽可能简单:)
编辑:
我有这个:(我不太懂)
public void getLocation() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
和这一个:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
getLocation();
}
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
location_lat = latitude;
location_long = longitude;
}
答案 0 :(得分:0)
将此权限添加到清单(API版本< 23)
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
声明两个变量来存储坐标和一个位置变量
Location location;
double latitude;
double longitude;
用这个
替换你的getLocation()方法public void getLocation(){
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
if (locationManager != null) {
try {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
} catch (SecurityException e) {
}
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
获取坐标后,在电子邮件正文中传递此内容。
...
i.putExtra(Intent.EXTRA_TEXT , "Longitude "+longitude+", Latitude "+latitude);
...