簡體   English   中英

存儲地圖位置-android

[英]Storing a Map Location - android

我正在嘗試獲取GPS位置並將其解析為字符串,以依次將其解析為可點擊的網址。 我已經完成了獲取位置的代碼,該方法可以正常工作,但是由於獲取位置可能需要一些時間,因此在解析位置之前,String已在執行。 我已經嘗試過處理對話框對話框,但我無法使其正常工作。

見代碼

公共類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坐標3)這些坐標應傳遞給字符串4)隨坐標一起發送電子郵件(可以正常工作)5)如果選中此框,則會播放聲音(同樣有效)

我正在獲取坐標,但困難是在應用程序有機會獲取坐標之前,已調用電子郵件方法。 我不想使用lastKnownCo-ords。

1 / No. onCreate請求在將來的某個時間將位置分配給mLocListener,但是無論如何在返回onCreate之后。 onCreate還會啟動一個新的Activity以編寫電子郵件。 在htis點處未檢索到坐標。

2 /否。 收到GPS坐標時,將打開一個對話框。

3 /不完全是。 坐標將傳遞到地址解析器,然后轉換為地址。 我不知道為什么,因為您不使用此地址(顯示除外)

4 /否。coords字符串以3 /創建,但電子郵件以1 /發送,因此比以前早得多。

您實際要做的是收到坐標后即在onLocationChanged方法中發送電子郵件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM