2011-05-25 70 views
2

好的,我已經嘗試Nicholas的解決方案,但仍然收到強制關閉,如果我按下後退按鈕,因爲應用程序搜索gps fix.so im上傳所有的代碼和文件(除icon.png和pen.png在drawables中)...這樣就可以從這裏複製粘貼代碼,看看發生了什麼。我的main.xml:stop gps on exiting app

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/icon" 
    > 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 
<Button android:text="Start" 
android:id="@+id/Button01" 
android:layout_width="120px" 
android:layout_height="70px" 
android:layout_gravity = "center_horizontal" 
android:textSize="20px" 
android:layout_marginTop="150px" 
android:clickable="true" 
></Button> 

</LinearLayout> 

接下來我的主要活動(GpsLocEx.java)

package com.example.gpslocex; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class GpsLocEx extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button start = (Button) findViewById(R.id.Button01); 
     start.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      Intent map = new Intent(view.getContext(), com.example.gpslocex.ShowMap.class); 
      startActivityForResult(map, 0); 

      } 

     }); 


} 
    } 

下一個我gps.xml

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

    <com.google.android.maps.MapView 
     android:id="@+id/mapview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:clickable="true" 
     android:apiKey="06ZLf-HkdkRMb513KS2ZRljmMQL0bntlyMJ-rOQ" 
    /> 

</RelativeLayout> 

下一個ShowMap.java

package com.example.gpslocex; 

import java.util.List; 

import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Point; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 
import com.google.android.maps.Overlay; 

public class ShowMap extends MapActivity { 

    private MapController mapController; 
    private MapView mapView; 
    private LocationManager locationManager; 
    private GeoUpdateHandler geoUpdateHandler; 

    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.gps); 
      mapView = (MapView) findViewById(R.id.mapview); 
      mapView.setBuiltInZoomControls(true); 
      mapView.setSatellite(true); 
      mapController = mapView.getController(); 
      mapController.setZoom(14); // Zoom 1 is world view 
      geoUpdateHandler = new GeoUpdateHandler(); 

      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
      if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
       createGpsDisabledAlert(); 
      } else { 
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 
         0,geoUpdateHandler); 

      } 

     } 
    private void createGpsDisabledAlert() { 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder 
        .setMessage(
          "Your GPS is disabled! Would you like to enable it?") 
        .setCancelable(false).setPositiveButton("Enable GPS", 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int id) { 
            showGpsOptions(); 
           } 
          }); 
      builder.setNegativeButton("Do nothing", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        }); 
      AlertDialog alert = builder.create(); 
      alert.show(); 
     } 

    private void showGpsOptions() { 
      Intent gpsOptionsIntent = new Intent(
        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      startActivity(gpsOptionsIntent); 
     } 


    @Override 
    protected boolean isRouteDisplayed() { 
     // TODO Auto-generated method stub 
     return false; 
    } 


    public class GeoUpdateHandler implements LocationListener { 

     @Override 
     public void onLocationChanged(Location location) { 
      int lat = (int) (location.getLatitude() * 1E6); 
      int lng = (int) (location.getLongitude() * 1E6); 
      GeoPoint point = new GeoPoint(lat, lng); 
      mapController.animateTo(point); // mapController.setCenter(point); 
      MapOverlay mapOverlay = new MapOverlay(); 
       mapOverlay.setPointToDraw(point); 
       List<Overlay> listOfOverlays = mapView.getOverlays(); 
       listOfOverlays.clear(); 
       listOfOverlays.add(mapOverlay); 

     } 

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

     } 

     @Override 
     public void onProviderEnabled(String provider) { 
     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
     } 
    } 


    class MapOverlay extends Overlay 
    { 
     private GeoPoint pointToDraw; 

     public void setPointToDraw(GeoPoint point) { 
     pointToDraw = point; 
     } 

     public GeoPoint getPointToDraw() { 
     return pointToDraw; 
     } 

     @Override 
     public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { 
     super.draw(canvas, mapView, shadow);   

     // convert point to pixels 
     Point screenPts = new Point(); 
     mapView.getProjection().toPixels(pointToDraw, screenPts); 

     // add marker 
     Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pen); 
     canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 128, null);  
     return true; 
     } 




    } 
    @Override 
    public void onPause() { 
     locationManager.removeUpdates(geoUpdateHandler); 
    } 


} 

next strings.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="hello">Version-1.0</string> 
    <string name="app_name">GpsLocEx</string> 

</resources> 

最後的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.example.gpslocex" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <uses-library android:name="com.google.android.maps" /> 
     <activity android:name=".GpsLocEx" 
        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=".ShowMap"></activity> 

</application> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="ACCESS_COARSE_LOCATION" /> 

</manifest> 

回答

15

Activity不再位於頂端時,您可以使用Android lifecycle methods來關閉GPS。

使用onPause(),onStop()或onDestroy()從您的locationManager中刪除更新。事情是這樣的:

@Override 
public void onPause() { 
    locationManager.removeUpdates(myLocationListener); 
} 

編輯:

您的onPause()方法一個ClassCastException。你不能投this(你的Activity)到LocationListener(至少不是讓你的Activity執行LocationListener)。

在您的代碼中,您將創建一個new GeoUpdateHandler()。首先,你應該創建一個實例變量,它是你的GeoUpdateHandler。下面爲LocationManager您的實例變量,創建一個這樣的:

private GeoUpdateHandler geoUpdateHandler; 

你應該保持一個手柄,這在onCreate()方法:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    //...some code here... 
    geoUpdateHandler = new GeoUpdateHandler(); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 
         0,geoUpdateHandler); 
    //...maybe some more code here... 
} 

然後在的onPause,通過geoUpdateHandler到的removeUpdates功能:

@Override 
public void onPause() { 
    locationManager.removeUpdates(geoUpdateHandler); 
} 

編輯2:

Stacktrace說明了這一切:

android.app.SuperNotCalledException: Activity {com.example.gpslocnav6ex/com.example.gpslocnav6ex.ShowMap} did not call through to super.onPause() 

通過超級呼叫。在onPause()在你的onPause()

@Override 
public void onPause() { 
    super.onPause(); 
    locationManager.removeUpdates(geoUpdateHandler); 
} 
+0

感謝尼古拉斯,但我越來越逼近我的宏達... – 2011-05-26 06:30:00

+0

也許你可以修改你的問題與力量關閉stacktrace。 – 2011-05-26 12:41:48

+0

嗨我編輯我的問題與強制關閉期間的logcat消息... – 2011-05-27 03:41:28

2

試試這個locationManager.removeUpdates(myLocationListener);

你應該在onPause寫()

@Override 
    protected void onPause() 
    { 
     // TODO Auto-generated method stub 
     super.onPause(); 
       locationManager.removeUpdates(myLocationListener); 
    } 

這將關閉GPS,當你的應用程序的退出。

+0

感謝sujit但我m到處強制關閉時,我在我的onPause方法中使用此代碼... – 2011-05-25 06:27:00

+0

什麼是你所得到的錯誤。你可以把它粘貼在這裏嗎? – Sujit 2011-05-25 06:30:46

+0

sujit我越來越逼近我的HTC ... – 2011-05-26 06:28:53