0

我正在尝试获取 GPS 位置并将其解析为字符串,然后将其解析为可点击的 URL。我已经完成了获取位置的代码,这很有效,但是因为获取位置可能需要一段时间,所以字符串在解析位置之前就已经执行了。我试过搞乱进程对话框等,但我无法让它工作。

见代码

公共类 ConfirmScreen 扩展 Activity{

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 坐标 3) 这些坐标应传递给字符串 4) 与坐标一起发送电子邮件(这工作正常) 5)如果选中该框,则会播放声音(同样有效)

我得到了坐标,但困难在于在应用程序有机会获得坐标之前调用了电子邮件方法。我不想使用 lastKnownCo-ords。

4

1 回答 1

1

1/ 否。onCreate 请求在未来某个时间点将位置提供给 mLocListener,但无论如何在 onCreate 返回之后。onCreate 还会启动一个新的 Activity 来写一封电子邮件。在 htis 点没有检索到坐标。

2/ 否。当接收到 GPS 坐标时,会打开一个对话框。

3/ 不完全是。坐标被传递给地理编码器并转换为地址。我不知道为什么,因为你不使用这个地址(除了显示)

4/ 不。坐标字符串是在 3/ 中创建的,但电子邮件是在 1/ 中发送的,因此之前很多。

你真正想做的是在收到坐标发送邮件,也就是在 onLocationChanged 方法中。

于 2012-10-02T14:08:27.650 回答