코드부터 보여드리겠습니다.
package org.jsb.busgod.busgod;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.location.LocationManager;
import android.os.Environment;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
public class LocationService extends Service {
private static final String TAG = "LocationService";
private LocationManager locationManager;
private LocationReceiver locationReceiver;
private ReStartServiceReceiver reStartServiceReceiver;
private HashMap<Integer,PendingIntent> locationList;
private String intentKey = "org.jsb.busgod.busgod";
private PendingIntent pendingIntent;
private int id;
private double lat;
private double lng;
private float radius;
private long expiration;
private int type;
private String lock = "aaa";
public LocationService() {
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG,"onCreate Called.");
locationList = new HashMap<Integer,PendingIntent>();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG,"onStartCommand Called.");
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationReceiver = new LocationReceiver(intentKey);
registerReceiver(locationReceiver, locationReceiver.getFilter());
int size = locationList.size();
Log.d(TAG,"Size : " + size);
id = intent.getIntExtra("id",0);
lat = intent.getDoubleExtra("lat",0.0);
lng = intent.getDoubleExtra("lng",0.0);
radius = intent.getFloatExtra("radius", 0);
expiration = intent.getLongExtra("expiration",0);
type = intent.getIntExtra("type",2);
switch (type){
case 0:
register(id, lat, lng, radius, expiration);
break;
case 1:
deleteLocation(id);
break;
}
return super.onStartCommand(intent, flags, startId);
}
public void register(int id, double lat, double lng, float radius, long expiration){
Log.d(TAG,"id : " + id + ", lat : " + lat + ", lng : " + lng + ", radius : " + radius + ", expiration : " + expiration);
Intent proximityIntent = new Intent(intentKey);
proximityIntent.putExtra("id",id);
pendingIntent = PendingIntent.getBroadcast(this,id,proximityIntent,PendingIntent.FLAG_CANCEL_CURRENT);
locationManager.addProximityAlert(lat,lng,radius,expiration,pendingIntent);
locationList.put(id, pendingIntent);
}
public void deleteLocation(int id){
if(locationList.size() != 0 ){
Log.d(TAG,"deleteLocation id : " + id);
PendingIntent curIntent = locationList.get(id);
locationManager.removeProximityAlert(curIntent);
locationList.remove(id);
}
}
private void unregister(){
Log.d(TAG, "unregister Called.");
unregisterReceiver(locationReceiver);
locationReceiver = null;
unregisterReceiver(reStartServiceReceiver);
reStartServiceReceiver = null;
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy Called.");
reStartServiceReceiver = new ReStartServiceReceiver();
IntentFilter filter = new IntentFilter("RESTARTSERVICE");
registerReceiver(reStartServiceReceiver,filter);
Intent intent = new Intent(LocationService.this,ReStartServiceReceiver.class);
intent.putExtra("locationList",locationList);
sendBroadcast(intent);
unregister();
}
}
코드상에서 2개의 리시버를 사용하고있습니다. 리시버는 각각 class로 되어있고요. unregister도 해줬는데 자꾸 오류가 나네요.
Error : android.app.IntentReceiverLeaked: Service org.jsb.busgod.busgod.LocationService has leaked IntentReceiver org.jsb.busgod.busgod.LocationReceiver@412bab08 that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:763)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:567)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1043)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1030)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1024)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:341)
at org.jsb.busgod.busgod.LocationService.onStartCommand(LocationService.java:55)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2359)
at android.app.ActivityThread.access$1900(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)