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

pendingintent 값 삭제 질문드립니다.

0 추천

현재 서비스에서 pendingintent를 사용중입니다. onStartCommand() 에서 intent로 값을 받아 register() 라는 임의로정의한 메서드로 값을 보내고 메서드안에 정의된 pendingintent 로 동적등록 하고 있습니다. 

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG,"onStartCommand Called.");
        int restart = intent.getIntExtra("restart",0);
            switch (restart) {
                case 1:
                    locationList = (HashMap<Integer, PendingIntent>) intent.getSerializableExtra("locationList");
                    Log.d(TAG, "LocationList : " + locationList);
                    break;
            }

        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        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 Service.START_REDELIVER_INTENT;
    }


    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("org.jsb.busgod.busgod");
        proximityIntent.setAction("org.jsb.busgod.busgod");
        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);
    }

여기서는 pendingintent에 들어간 값을 관리하기 위해서 따로 hashmap 을 정의해서 Integer,pendingintent 로 저장중입니다. 근데 이 구조로 하니까 서비스가 강제로 죽었을때와 핸드폰부팅시 service를 새로시작시킬때 hashmap안에 있는 값을 유지하는게 너무힘듭니다.강제로죽었을때는 리시버로 어떻게 했는데 핸드폰부팅시 서비스를 실행시킬때는 답이 안나오더군요. 그래서 혹시 pendingintent 에 입력된 값을 직접적으로 관리하는방법이 없는지 물어보기 위해서 질문글을 올리게되었습니다. 서비스를 새로 실행시키면 oncreate가 호출되서 계속 hashmap이 초기화되면서 pendingintent 에 입력된값을 관리할수가없습니다. 제발 도와주세요.. 정말 힘이드네요 ㅠㅠ 

우랴 (3,680 포인트) 님이 2014년 7월 1일 질문

1개의 답변

0 추천
핸드폰 재부팅시에는 초기화 되는게 당연하고요..

sharedpreference나 db를 이용하셔야할듯 합니다.
노예의집 (23,370 포인트) 님이 2014년 7월 1일 답변
hashmap<Integer,PendingIntent>형태를 sharedpreference 에 어떻게 저장해야되는건가요? sharedpreference 는 위에형태를 저장할수 있는 타입이 없더라구요.
sharedpreference는 primitive 타입밖에 못다룹니다.
Parcelable 같은것도 안되고요..
sharedpreference 자체가 key, value로 이루어진 거라 hashmap과 비슷합니다.
pendingintent 자체를 저장 할 수는 없고, String 으로 변환해서 저장하는 방식을 사용하셔야 하는데 json을 많이 씁니다.
어떻게 변환해서 값들을 만들어 낼 지는 생각을 좀 해 보셔야할듯 합니다.
json으로 변환시켜서 string로 저장시키고 get할때 다시 pendingintent로 변환이 가능한가요?
get으로 얻은 string 데이터를 기반으로 hashmap<Integer,PendingIntent>형태를 다시 만들어서 사용하셔야합니다.
상당히 어렵겠네요... 일단 해보겠습니다 감사드립니다!
...