我正在使用服务从该位置获取更新,它不是 IntentService,但日志显示Activity 泄露了最初在此处注册的 IntentReceiver。您是否错过了对 unregisterReceiver() 的调用? 我不使用接收器,所以我不注册或取消注册。那么,这到底是怎么回事?
我粘贴我的服务代码:
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
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 UpdateService2 extends Service {
private LocationManager locManager;
private LocationListener locListener;
private Location loc;
public static int UPDATE_TIME = 30000;
public static long MAX_TIME = 600000;
public static long waited = 0;
boolean active = true;
String TAG = "UpdateService2";
Thread myThread;
SharedPreferences prefs;
SharedPreferences.Editor editor;
@Override
public IBinder onBind(Intent arg0) {
Log.d(TAG, "onBind");
return null;
}
public void onCreate() {
Log.d(TAG, "onCreate");
SharedPreferences prefs = getSharedPreferences(MyConstants.MY_PREFERENCES,Context.MODE_PRIVATE);
editor = prefs.edit();
startGettingLocation();
Log.d("UpdateService","Thread - active:"+active+", maxTime: "+MAX_TIME);
myThread = new Thread() {
public void run(){
Log.d("UpdateService","run");
try {
waited = 0;
Log.d("UpdateService","Thread - active:"+active+", maxTime: "+MAX_TIME+", waited: "+waited);
while(active && (waited < MAX_TIME)) {
sleep(10000);
if(active) {
waited += 10000;
Log.d("UpdateService","Thread update: "+waited/1000+" seg");
}
}
} catch(InterruptedException e) {
Log.d("UpdateService","Exception: "+e.toString());
} finally {
interrupt();
}
}
};
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
active = false;
}
@Override
public void onStart(Intent intent, int startid) {
myThread.start();
Log.d(TAG, "onStart");
}
private void startGettingLocation(){
try {
locListener = new LocationListener() {
public void onLocationChanged(Location location) {
updatePosition(location);
Log.d("UpdateService","Update location - Lat:"+location.getLatitude()+", Lon:"+location.getLongitude());
}
public void onProviderDisabled(String provider){
}
public void onProviderEnabled(String provider){
}
public void onStatusChanged(String provider, int status, Bundle extras){
}
};
locManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, UPDATE_TIME, 0, locListener);
loc = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (loc != null) {
updatePosition(loc);
}
} catch (Exception e){
Log.d("UpdateService", e.toString());
}
}
private void updatePosition(Location loc) {
if(loc != null) {
Double dLat = loc.getLatitude();
Double dLon = loc.getLatitude();
editor.putInt(MyConstants.PREFERENCES_LAT, dLat.intValue());
editor.putInt(MyConstants.PREFERENCES_LON, dLon.intValue());
editor.commit();
}
}
}
然后,来自活动 onCreate 的调用是这样的:
msgIntent = new Intent(this, UpdateService2.class);
startService(msgIntent);
来自 onDestroy 的调用是这样的:
stopService(msgIntent);