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

Receiver사용중 오류가 있습니다..!

0 추천

코드부터 보여드리겠습니다.

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)
우랴 (3,680 포인트) 님이 2014년 6월 30일 질문

1개의 답변

0 추천

registerReceiver는 onStartCommand에서 하고 

 unregisterReceiver 는 onDestroy에서 하는군요

 

regiesterReceiver를 반복적으로 할 필요가 없으시면

onCreate에서 하세요.

 

이미 리시버가 등록되어 있는데 또 등록되어서 발생한 것이네요.

오류 메시지를 잘 읽어보시면 답이 있어요.

 

org.jsb.busgod.busgod.LocationReceiver@412bab08 that was originally registered here. Are you missing a call to unregisterReceiver()?

org.jsb.busgod.busgod.LocationService.onStartCommand(LocationService.java:55)

익명사용자 님이 2014년 7월 1일 답변
답변감사드립니다! 일단 제가 급해서 매니페스트에서 선언해서 사용하고있습니다.
근데 님 말씀대로 onStartCommand()에서 선언한게 문제가 될 것 같네요.
다음에는 onCreate() 에서 선언해서 사용해보겠습니다. 다시 한번 답변감사드립니다 ^^
...