AsyncTask& Maps

时间:2016-07-12 14:01:32

标签: android android-asynctask splash-screen

当我启动我的应用程序时,它通过AsyncTask类向db提交查询。当应用程序获取数据时,我得到了一个白色屏幕,当完成与db的通信时,将显示一个地图,其中包含从服务器获取的所有标记。我想知道是否有办法使用onProgressUpdate显示启动画面。或者,如果有更好的方式来显示启动画面。我选择的图像是一个jpeg文件。

3 个答案:

答案 0 :(得分:0)

OnLocationChanged我调用asyncTask:

AsyncTaskRunner run = new AsyncTaskRunner();
            run.execute (lat, lng);

所以asyncTask的代码:

 class AsyncTaskRunner extends AsyncTask<Double, Void, List<Map<String, Double>>> {

    @Override
    protected List<Map<String, Double>> doInBackground(Double... coords) {
        try {

            URL url = new URL(....);

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.connect();


            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;


            while ((inputLine = in.readLine()) != null) {

                JSONObject jsonObject = new JSONObject(inputLine);
                String code = jsonObject.getString("code");

                Log.i("FontaneDB", code);

                JSONArray data = jsonObject.getJSONArray("data");

                for (int i = 0; i < data.length(); i++) {
                    JSONObject fontana = data.getJSONObject(i);

                    //Iterate through the elements of the array i
                    Map<String, Double> font = new HashMap<String, Double>();

                    font.put("latitudine", Double.parseDouble(fontana.getString("latitudine")));
                    font.put("longitudine", Double.parseDouble(fontana.getString("longitudine")));
                    result.add(font);
                }
            }


            in.close();
        }
        catch (Exception e) {
            Log.e("FontaneDB", e.toString());
        }
    return result;

    }

    @Override
    protected void onPostExecute(List<Map<String, Double>> maps) {

        int distance, contatore = 1;
        Location loc = new Location(LocationManager.NETWORK_PROVIDER);
        if (maps.isEmpty()) {
            Toast.makeText(context, "Non ci sono fontane nelle vicinanze", Toast.LENGTH_LONG).show();
        }
        else {
            for (Map<String, Double> fontana : maps) {

                loc.setLatitude(fontana.get("latitudine"));
                loc.setLongitude(fontana.get("longitudine"));
                distance = Math.round(mCurrentLocation.distanceTo(loc));

                Log.i("FontaneDB", "loc: "+loc.toString());
                Log.i("FontaneDB", "mcurr "+ mCurrentLocation.distanceTo(loc));
                Log.i("Distanza", "distanza: "+distance);

                Log.i("Prova", "Latitudine: " + fontana.get("latitudine").toString());


                if (contatore == 1) {
                    mMap.addMarker(new MarkerOptions().
                            position(new LatLng(fontana.get("latitudine"), fontana.get("longitudine"))).
                            icon(BitmapDescriptorFactory.
                                    defaultMarker(BitmapDescriptorFactory.HUE_BLUE)).title(distance + "m")).showInfoWindow();
                    contatore++;
                }
                else {
                    mMap.addMarker(new MarkerOptions().
                            position(new LatLng(fontana.get("latitudine"), fontana.get("longitudine"))).
                            icon(BitmapDescriptorFactory.
                                    defaultMarker(BitmapDescriptorFactory.HUE_BLUE)).title(distance + "m"));
                }
            }
        }


    }
}

没有使用onProgressUpdate方法..我想将它用于splashscreen。如果可能的话,我怎么能这样做呢?

答案 1 :(得分:0)

你可以这样做,修改你的 - 对我来说,对于同样类型的问题,它可以正常工作。

私有类Waiter扩展了AsyncTask {

    @Override
    protected Void doInBackground(Void... params) {

        try {
            // do your background job like load Map 
        } catch (Exception e) {

            e.printStackTrace();
        }finally{

         }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {

        //Show Map here or pass Map Activity after parsing completion 
    }
}

答案 2 :(得分:0)

这是一个可能的(未经测试的)解决方案,有一个SplashActivity.java将地图传递给MainActivity.java,然后解析并显示地图:

<强> SplashActivity.java

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        new AsyncTaskRunner.execute();
    }

    public void mapsRetrieved(List<Map<String, Double>> maps) {
        Intent intent = new Intent(this, MainActivity.class); //adjust class name as needed
        intent.putExtra("maps", maps);
        startActivity(intent);
    }

    private class AsyncTaskRunner extends AsyncTask<Double, Void, List<Map<String, Double>>> {
    //YOUR CODE
        @Override
        protected void onPostExecute(List<Map<String, Double>> maps) {
            mapsRetrieved(maps);
        }
    }
}

在activity_splash.xml中,将.jpeg导入为可绘制资源并替换 {YOUR_BACKGROUND_HERE}

<强> activity_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
<!-- REPLACE {YOUR_BACKGROUND_HERE} -->
android:background="@drawable/{YOUR_BACKGROUND_HERE}"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.daose.watinfo.SplashActivity"/>

您可以像这样检索地图

<强> MainActivity.java

public class EventListActivity extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        List<Map<String, Double>> maps = (List<Map<String, Double>>) getIntent().getSerializableExtra("maps");
        //YOUR ORIGINAL POST-EXECUTE CODE BELOW
    }
}

然后,在AndroidManifest.xml中,更改应用程序打开时启动的活动。

<强>的AndroidManifest.xml

<application
    <!-- other stuff -->
    <activity android:name=".SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <!-- other stuff -->
</application>