3

ユーザーの場所を取得中に進行状況ダイアログを表示する asynctask を作成しました。この asynctask を 30 秒間実行したいのですが、この 30 秒間でユーザーの場所が見つからない場合は、タスクを強制終了してエラー メッセージを表示したいと思います。

これまでの私のコードは次のようなものです:

userLocation = new AsyncTask<Void, Void, Void>() {

            private ProgressDialog locationDialog = new ProgressDialog(MainActivity.this);

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                locationDialog.setMessage(getResources().getString(R.string.getting_location));
                locationDialog.setCancelable(false);
                locationDialog.setIndeterminate(true);
                locationDialog.show();
            }

            @Override
            protected Void doInBackground(Void... params) {
                    try {
                        latitude = locationProvider.getLatitude();
                        longitude = locationProvider.getLongitude();
                        Thread.sleep(10000);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
            //  getLocation.cancel(true);
                if (latitude != 0 && longitude != 0) {
                    locationDialog.dismiss();
                    getData();
                } else {
                    locationDialog.dismiss();

                    alertDialog.show();

                    Log.d("Couldn't get location", "Couldn't get location");
                }
            }
        };
        userLocation.execute((Void[])null);

30 秒後の緯度と経度が 0 の場合、asynctask を強制終了して何らかのエラー メッセージを表示するようにコードを編集するにはどうすればよいですか。何か案は?

4

1 に答える 1