마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

브로드캐스트리시버 unregister 사용질문드립니다.

0 추천

현재 서비스에서 브로드캐스트리시버를 2개 사용중입니다. 각각 다른 클래스로 만들었습니다. 하나는 자바코드로 선언을하였고 하나는 manifest에서 선언하였습니다. 근데 문제는 서비스 destroy() 쪽에 unregister()를 선언하였는데 자꾸 릭 오류가 뜨네요.. 하 미치겠습니다.선언을했는데도 뜨니까 아무리봐도 답이 안나오네요.. 도와주세요 ㅠㅠ 소스 보여드리겠습니다.

package org.jsb.busgod.busgod;

public class LocationService extends Service {

    public LocationService() {

    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate Called.");

        unregisterRestartAlarm();
        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 registerRestartAlarm(){
        Intent intent = new Intent(LocationService.this,ReStartServiceReceiver.class);
        intent.setAction("RESTART");
        intent.putExtra("locationList",locationList);
        PendingIntent sender = PendingIntent.getBroadcast(LocationService.this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        long firstTime = SystemClock.elapsedRealtime();
        firstTime += 10 * 1000;
        AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime,10*1000,sender);
    }

    private void unregisterRestartAlarm(){
        Intent intent = new Intent(LocationService.this,ReStartServiceReceiver.class);
        intent.setAction("RESTART");
        PendingIntent sender = PendingIntent.getBroadcast(LocationService.this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
        am.cancel(sender);
    }

    private void unregister(){
        Log.d(TAG, "unregister Called.");

        unregisterReceiver(locationReceiver);
    }


    @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.");

        registerRestartAlarm();

        unregister();

    }
}

Error :     android.app.IntentReceiverLeaked: Service org.jsb.busgod.busgod.LocationService has leaked IntentReceiver org.jsb.busgod.busgod.LocationReceiver@412a8178 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:57)
            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)
우랴 (3,680 포인트) 님이 2014년 6월 30일 질문

1개의 답변

0 추천

onDestroy() 에

unregisterReceiver(locationReceiver);

이렇게 함 줘 보셔요

nicehee (73,100 포인트) 님이 2014년 6월 30일 답변
...