【问题标题】:How to increase the speed to load an activity?如何提高加载活动的速度?
【发布时间】:2014-12-10 13:16:57
【问题描述】:

我正在开发一个 Android 应用程序,它使用 GoogleMaps。所以当我点击一个按钮时,我会加载一个显示谷歌地图的 registraPosizioneActivity。所以我注意到这个活动的加载速度很慢。有没有提高加载这个activity的速度的模式?

这是我的代码:

         protected void onCreate(Bundle savedInstanceState) {
            try{
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_recordposition);
                locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
                Criteria criteria = new Criteria();
                criteria.setAccuracy(Criteria.ACCURACY_FINE);
                provider = locationManager.getBestProvider(criteria, false);
                locationManager.requestLocationUpdates(provider, 1000, 0, this);
                settaMappa();
                if(myPosition==null){
                    pDialog = ProgressDialog.show(this, "Attendere ...", "Localizzazione in corso ...", true);
                    pDialog.setCancelable(false);
                }
            }catch(Exception e){
                Log.e("Errore onCreate:",e.getMessage());
                Utility.generateNoteOnSD(fileName,e.getMessage());
            }
        }

       public void settaMappa(){
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map2)).getMap();
            mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
            mMap.setMyLocationEnabled(true);
        }

这是activity_recordposition.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical"
    tools:context=".registraPosizioneActivity"
    android:gravity="center">


    <fragment 
      android:name="com.google.android.gms.maps.SupportMapFragment"
      android:id="@+id/map2"
      android:layout_height="0dp"
      android:layout_weight="10"
      android:layout_width="match_parent"
      class="com.google.android.gms.maps.SupportMapFragment"
      />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
        android:id="@+id/buttonR"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="0.20"
        android:layout_margin="20dp"
        android:background="#0080c2"
        android:textColor="#FFF"
        android:textSize="20sp"
        android:onClick="registraPunto"
        android:text="@string/registraPunto" />

    <Button
        android:id="@+id/buttonSava"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="0.20"
        android:layout_margin="20dp"
        android:background="#0080c2"
        android:textColor="#FFF"
        android:textSize="20sp"
        android:onClick="savaAreaTracciata"
        android:text="@string/terminaRegistrazione" />
    </LinearLayout>

【问题讨论】:

  • 您是否真的检查过主要生命周期方法的活动需要多长时间?
  • 我已经注释了 onCreate 方法的所有代码,但是加载我的活动的速度很慢。

标签: android google-maps android-activity


【解决方案1】:

在我看来,您似乎在 onCreate 方法中做了很多处理。尝试将其中的一些处理移动到异步线程中。我相信这会大大增加加载时间。

所以,

  • 加载活动,在 onCreate 中启动异步任务
  • 异步线程在活动完成后执行所需的处理 加载:)

【讨论】:

  • 看起来他并没有做很多事情,只是设置了一个位置管理器并获取了地图。没什么
  • 嗨,这可能不是很多,但他将精度设置为很好,以清楚地表明他正在测试它的设备需要一段时间才能获得 GPS 坐标,因此活动似乎没有加载太多快速地。如果他运行活动并在另一个线程(异步)中进行 GPS 处理。该活动几乎会立即出现,并且异步线程在后台运行:)。它并没有变得更快,但是,活动加载得更快,这正是他想要的。
  • 获取 GPS 锁定与加载活动的速度无关。无需获得 GPS 锁定即可加载活动
  • 好吧,抱歉,我想是因为 GPS 锁定在 onCreate 方法中,所以直到 GPS 锁定成功后才加载活动。哎呀:)
  • 我应该删除我的答案吗?
猜你喜欢
  • 2014-12-03
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-07
  • 1970-01-01
相关资源
最近更新 更多