0

I'm trying to make my app wait till the current location is found. I've tried few different ways using Threads and all have failed really. I was using wait() and notify() but application just hung and never found the current location.

I amen't using google map api as it is not part of the application. Does anyone have any ideas how to do this or examples.

EDIT : The Thread I was using did not start till the user pressed a button then within onLocationChanged other data is processed e.g. adding the new location to ArrayList, Calculate the distance between the current and last Location as well as the Time taken to get to the new location

4

2 に答える 2

0

したがって、あなたが何をしたいのかを正しく理解していれば、 で別のスレッドを作成することは避けますonClick()。代わりに、onClick()場所を要求し、進行状況ダイアログを表示して、戻る必要があります。あなたがやりたい仕事は新しい場所を受け取った後に行われるので、私はそこで AsyncTask を開始します。次に、AsyncTask が終了したら、最後にダイアログ ボックスを削除します (削除すると、コントロールがユーザーに返されます)。

コードは通常役立つので、これをonCreate()またはどこにでも配置します。

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        listener.refresh();
    }
});

これを LocationListener に入れます。

public void refresh() {
    myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    myDialog = new ProgressDialog(myContext);
    myDialog.setIndeterminate(true);
    myDialog.show();
}

@Override
public void onLocationChanged(Location location) {
    // now do work with your location, 
    // which your probably want to do in a different thread
    new MyAsyncTask().execute(new Location[] { location });
}

次に、次のような AsyncTask が必要です。

class MyAsyncTask extends AsyncTask<Location, Void, Void> {
    @Override
    protected Void doInBackground(Location... location) {
        // start doing your distance/directions/etc work here
        return null;
    }


    @Override
    protected void onPostExecute(Void v) {
        // this gets called automatically when you're done, 
        // so release the dialog box
        myDialog.dismiss();
        myDialog = null;
    }
}
于 2012-01-30T22:49:26.083 に答える