我正在尝试获取GPS位置并将其解析为String,然后将其解析为可点击的URL。我已经完成了获取位置的代码,但是由于获取位置可能需要一段时间,因此String在解析位置之前执行。我已经尝试过使用Process Dialogs等,但是我无法让它工作。
参见代码
公共类ConfirmScreen扩展了活动{
String mapCoord = "http://maps.google.com";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirm_screen);
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mLocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, mLocListener);
sendEmail();
playSound();
}
public void backHome(View view)
{
Intent intent = new Intent (this, MainScreen.class);
startActivity(intent);
}
// Method to start playing and looping a sound.
public void playSound()
{
MediaPlayer clickSound = MediaPlayer.create(this, R.raw.warning);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Boolean soundCheck = sp.getBoolean("SOUND", false);
if (soundCheck)
{
clickSound.start();
}
}// method end
public void sendEmail()
{
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String nameValue = sp.getString("NAME", "failed to get name");
String emailValue = sp.getString("EMAIL", "failed to get email");
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{emailValue});
i.putExtra(Intent.EXTRA_SUBJECT, "Email sent from DON'T PANIC - A Chris O'Brien Project");
i.putExtra(Intent.EXTRA_TEXT, "Hi there\n" + nameValue + " is in mortal danger. Please see the co-ords attached and run to their rescue!" +
" If you don't see any co-ords, they didn't check the box and assume you know where they are.\nKind Regards\nDon't Panic! \n\n\n" + mapCoord);
try
{ startActivity(Intent.createChooser(i, "Send mail...."));
}
catch (android.content.ActivityNotFoundException ex){
Toast.makeText(ConfirmScreen.this, "There are no email clients installed or set up", Toast.LENGTH_SHORT).show();
}
}
public class MyAsyncTask extends AsyncTask<Void, Void, Result>{
private Activity activity;
private ProgressDialog progressDialog;
private double longitude;
private double latitude;
private String countryCode;
public MyAsyncTask(Activity activity, double longitude, double latitude) {
super();
this.activity = activity;
this.longitude = longitude;
this.latitude = latitude;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
ProgressDialog.show(activity, "", "Looking for GPS satellites...");
}
@Override
protected Result doInBackground(Void... v) {
Geocoder geocoder = new Geocoder(activity, Locale.getDefault());
List<Address> addresses;
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
countryCode = addresses.get(0).getAddressLine(2);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(activity, "Location is not available, please try again later.", Toast.LENGTH_LONG).show();
}
if(countryCode==null){
Toast.makeText(activity, "Location is not available, please try again later.", Toast.LENGTH_LONG).show();
}
Toast.makeText(activity, countryCode, Toast.LENGTH_LONG).show();
return null;
}
@Override
protected void onPostExecute(Result result) {
progressDialog.dismiss();
Toast.makeText(activity.getApplicationContext(), "Finished.",
Toast.LENGTH_LONG).show();
}
}
//Location Listener
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location) {
double lng = location.getLatitude();
double lat = location.getLongitude();
MyAsyncTask task = new MyAsyncTask(ConfirmScreen.this, lng, lat);
task.execute();
Toast.makeText(getApplicationContext(), "Done :D", Toast.LENGTH_LONG).show();
String text = "Current Location is \nLat: " + lng + " \nLng: " + lat;
mapCoord = Double.toString(lng) + " " + Double.toString(lat);
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) {
// TODO Auto-generated method stub
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_confirm_screen, menu);
return true;
}
}
编译没有错误,但它不符合我的意图。我一直在阅读别人的代码并尝试自己实现,但我可能会弄乱一些东西。
1)onCreate获取位置 2)应该弹出一个对话框,直到收到GPS co-ords 3)这些co-ords应传递给String 4)与合作伙伴发送电子邮件(此工作正常) 5)如果选中复选框,则播放声音(再次启用)
我得到了合作伙伴,但困难在于电子邮件方法在应用程序有机会获得合作之前被调用。我不想使用lastKnownCo-ords。
答案 0 :(得分:1)
1 /号onCreate要求在将来的某个时刻将位置提供给mLocListener,但无论如何在onCreate返回之后。 onCreate还会启动一个新的Activity来写一封电子邮件。在htis点没有检索到坐标。
2 /否。当收到 GPS坐标时,会打开一个对话框。
3 /完全没有。坐标传递给地理编码器并转换为地址。我不知道为什么你不使用这个地址(显示除外)
4 / No. coords字符串是在3 /中创建的,但电子邮件是以1 /发送的,因此很久以前。
您实际想要做的是在收到坐标后发送电子邮件,即在onLocationChanged方法中。