1

这让我头疼 - 任何帮助将不胜感激!

我从广播接收器启动服务:

public class SMSListener extends BroadcastReceiver {
@Override
public void onReceive (Context context, Intent intent) {
Intent i = new Intent(context, LocationService.class);
context.startService(i); 
}
}

我的 LocationService 代码是:

package com.someproject.common;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class LocationService extends Service {

final String TAG = "GPSlocation";
Timer timer1;
LocationManager lm;

String curPhoneNumber = "";
String curSender = "";
boolean gps_enabled = false;
boolean network_enabled = false;
long startTime = 0;
GPSlocator myLocation = new GPSlocator();

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.e(TAG, "onStartCommand");
    super.onStartCommand(intent, flags, startId);

    if (lm == null)
    lm = (LocationManager) this.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

    // exceptions will be thrown if provider is not permitted.
    try {gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);          } catch (Exception ex) {}
    try {network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);  } catch (Exception ex) {
    }

    // don't start listeners if no provider is enabled
    if (!gps_enabled && !network_enabled) {
        Log.i(TAG, "No listeners enabled"); // Raise notification
        return 0;
    }

    if (gps_enabled)
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
    if (network_enabled)
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);

    timer1 = new Timer();
    timer1.schedule(new GetFinalLocation(), 10000);

    return START_STICKY;
}


class GetFinalLocation extends TimerTask {
    @Override
    public void run() {
        Log.i(TAG, "GetFinalLocation");

        lm.removeUpdates(locationListenerGps);
        lm.removeUpdates(locationListenerNetwork);

        Location net_loc = null, gps_loc = null, final_loc = null;
        if (gps_enabled)                gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (network_enabled)            net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        // Do something with the location here...
    }
}

@Override
public void onCreate() {
    Log.e(TAG, "onCreate");
    super.onCreate();
}

@Override
public void onDestroy() {
    Log.e(TAG, "onDestroy");
    super.onDestroy();
}

LocationListener locationListenerGps = new LocationListener() {
    public void onLocationChanged(Location location) {
    }

    public void onProviderDisabled(String provider) {
    }

    public void onProviderEnabled(String provider) {
    }

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

LocationListener locationListenerNetwork = new LocationListener() {
    public void onLocationChanged(Location location) {
    }

    public void onProviderDisabled(String provider) {
    }

    public void onProviderEnabled(String provider) {
    }

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

}

该服务被调用,但导致模拟器崩溃。我想知道是否...

  1. 来自 BroadcastReceiver 的 Context 调用是正确的。
  2. 是否存在内存泄漏,因为似乎从未调用 LocationListeners(而且我从未到达 GetFinalLocation 调用)。
  3. 或者以这种方式使用 GPS 存在问题。

我花了一整天的时间解决这个问题,但无法弄清楚!

4

0 回答 0