私はスピードメーターを備えたアプリケーションに取り組んでいます。約 15 ~ 20 分間は正常に動作しますが、その後アプリがハングしてフリーズします。理由は何でしょうか?
でオーバーライドされるGPSManager
メソッドを呼び出すGPS データ用の別のクラスを作成しました。その方法では、速度と位置のすべての計算が行われます。onGPSUpdate()
MainActivity
以下は私のコードです:
public class MainActivity extends ActionBarActivity implements OnPageChangeListener,GPSCallback,OnClickListener
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
gpsManager = new GPSManager();
gpsManager.startListening(getApplicationContext());
gpsManager.setGPSCallback(this);
}
public void onGPSUpdate(Location location)
{
String directionString = "NORTH";
float speedDigital = 0 ;
if (location.hasSpeed())
{
speedDigital = location.getSpeed();
final String speedString = "" + roundDecimal(convertSpeed(speedDigital),2);
setSpeedText(R.id.textViewSpeed,speedString);
if (location.hasBearing())
{
int bearingAngle = (int) location.getBearing();
if ((bearingAngle > 337.5D) || (bearingAngle <= 22.5D))
directionString = "NORTH " + bearingAngle;
if ((bearingAngle > 22.5D) && (bearingAngle <= 67.5D))
directionString = "N-EAST "+ bearingAngle;
if ((bearingAngle > 67.5D) && (bearingAngle <= 112.5D))
directionString = "EAST "+ bearingAngle;
if ((bearingAngle > 112.5D) && (bearingAngle <= 157.5D))
directionString = "S-EAST "+ bearingAngle;
if ((bearingAngle > 157.5D) && (bearingAngle <= 202.5D))
directionString = "SOUTH "+ bearingAngle;
if ((bearingAngle > 202.5D) && (bearingAngle <= 247.5D))
directionString = "S-WEST "+ bearingAngle;
if ((bearingAngle > 247.5D) && (bearingAngle <= 292.5D))
directionString = "WEST "+ bearingAngle;
if ((bearingAngle > 292.5D) && (bearingAngle <= 337.5D))
directionString = "N-WEST "+ bearingAngle;
}
textViewCompass.setText(directionString);
}
else{
setSpeedText(R.id.textViewSpeed, "0.0");
}
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try {
textViewLocation.setText(currentLocation);
textViewLocation.setTextColor(Color.WHITE);
}
}
catch (IOException e){
e.printStackTrace();
}
}
private void setSpeedText(int textid,String text){
Double val = Double.parseDouble(text);
Integer intVal = val.intValue();
TextView tv = ((TextView)findViewById(textid));
tv.setText(""+intVal);
}
}
public class GPSManager{
private static final int gpsMinTime = 1000;
private static final int gpsMinDistance = 0;
private static LocationManager locationManager = null;
private static LocationListener locationListener = null;
private static GPSCallback gpsCallback = null;
public GPSManager(){
GPSManager.locationListener = new LocationListener(){
@Override
public void onLocationChanged(final Location location) {
if(GPSManager.gpsCallback != null){
GPSManager.gpsCallback.onGPSUpdate(location);
}
}
@Override
public void onProviderDisabled(final String provider){
Log.v("gps provider disabled ", provider);
}
@Override
public void onProviderEnabled(final String provider){
Log.v("gps provider enabled",provider);
}
@Override
public void onStatusChanged(final String provider, final int status, final Bundle extras){
Log.v("gps provider ststus", ""+status);
}
};
}
public GPSCallback getGPSCallback() {
return GPSManager.gpsCallback;
}
public void setGPSCallback(final GPSCallback gpsCallback){
GPSManager.gpsCallback = gpsCallback;
}
public void startListening(final Context context) {
if (GPSManager.locationManager == null){
GPSManager.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}
GPSManager.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0L, 0.0F, GPSManager.locationListener);
};
public void stopListening() {
try {
if (GPSManager.locationManager != null && GPSManager.locationListener != null) {
GPSManager.locationManager.removeUpdates(GPSManager.locationListener);
}
GPSManager.locationManager = null;
}
catch (final Exception ex){
}
}}