使用当前位置,系统崩溃在ICS上

时间:2012-05-18 01:11:06

标签: android maps location android-mapview android-4.0-ice-cream-sandwich

我正在尝试学习位置服务的基础知识,并使用“引脚”来标记位置。

我不确定这是最好的方法,还是替代方案。什么是更好的,或只是一个更简单的方式这样做?或者标准方式是什么?

此外,当我在ICS 4.0.3上运行此应用程序时,手机会在几分钟后冻结。它首先工作正常,并向我显示位置,但最终整个设备变得无法响应。我已经检查了logcat和last_kmsg,但我找不到任何有关导致此问题的提示。 Gingerbread 2.3.6运行代码没有问题(至少它没有崩溃超过15分钟,而ICS在1~5分钟内崩溃)。

它似乎不是一个例外,所以也许这是一些内存泄漏或内核恐慌?我不知道如何开始调试:\

任何帮助将不胜感激!

我使用以下代码:

Mapper.java:

public class Mapper extends MapActivity{

MapView mapView;
LocationManager lm;
LocationListener ll;
GeoPoint p;
MapController mc;
MapOverlay overlay;
List<Overlay> lol;

public void onCreate(Bundle x){
    super.onCreate(x);
    setContentView(R.layout.mapper);

    mapView = (MapView)findViewById(R.id.mapView);
    mapView.setBuiltInZoomControls(true);
    mc = mapView.getController();
    lol = mapView.getOverlays();
    overlay = new MapOverlay();
    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    ll = new MyLocationListener();

    mapView.invalidate();
}


private class MyLocationListener implements LocationListener{
    public void onLocationChanged(Location loc){
        if(loc!=null){
            int lat = (int)(loc.getLatitude() * 1E6);
            int lon = (int)(loc.getLongitude() * 1E6);
            p = new GeoPoint(lat,lon);

            mc.animateTo(p);
            mc.setZoom(18);

            lol.clear();
            lol.add(overlay);
        }
    }


    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
}

public void onResume(){
    super.onResume();

    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);
}

public void onPause(){
    super.onPause();

    lm.removeUpdates(ll);
}

protected boolean isRouteDisplayed(){
    return false;
}

private class MapOverlay extends com.google.android.maps.Overlay{
    public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when){
        super.draw(canvas, mv, shadow);

        Point sp = new Point();
        mv.getProjection().toPixels(p, sp);
        Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
        canvas.drawBitmap(pic, sp.x, sp.y, null);
        canvas.drawBitmap(pic, sp.x+60, sp.y, null);
        canvas.drawBitmap(pic, sp.x-60, sp.y, null);
        canvas.drawBitmap(pic, sp.x, sp.y+60, null);
        canvas.drawBitmap(pic, sp.x, sp.y-60, null);
        return true;
    }
}
}

mapper.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

   <com.google.android.maps.MapView
      android:id="@+id/mapView"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:apiKey="mykeyworks"
      android:clickable="true" />

</LinearLayout>

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jinworld"
android:versionCode="1"
android:versionName="1.0" android:installLocation="auto">

<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>"

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <uses-library android:name="com.google.android.maps" />
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SecondActivity"
        android:label="Second Activity" >
        <intent-filter>
            <action android:name="com.jinworld.SecondActivity" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Notify"
        android:label="DEVELOPERS" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Mapper"
        android:label="Locatio" >
        <intent-filter>
            <action android:name="com.jinworld.Mapper" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

</manifest>

1 个答案:

答案 0 :(得分:1)

您不需要这么多代码来显示您的位置,谷歌已经为此设置了叠加层。 试试这个,它可能会有所帮助:

public class Mapper extends MapActivity{
    com.google.android.maps.MyLocationOverlay myLocationOverlay;

    @Override
    protected void onResume() {
        super.onResume();
        myLocationOverlay.enableMyLocation();
    }

    @Override
    protected void onPause() {
        super.onPause();
        myLocationOverlay.disableMyLocation();
    }

    @Override
    public void onCreate(Bundle x){
        ...
        setupMyLocation();
        ...
    }

    protected void setupMyLocation(){
        myLocationOverlay = new MyLocationOverlay(this, mapView);
        myLocationOverlay.enableMyLocation();
        myLocationOverlay.runOnFirstFix(new Runnable() {
            public void run() {
                mapView.getOverlays().add(myLocationOverlay);
            }
        });
    }
}