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

addProximityAlert 를 서비스로 구현하는중에 문제가 생겼습니다. [closed]

+1 추천

안녕하세요. 

addProximityAlert를 서비스로 구현중에 문제가 생겨 이렇게 조언을 구하게 되었습니다. 

 

문제가 생기는 부분은 아래 소스의 

 mLocationManager.addProximityAlert(35.197007,  129.0859003, 200, -1, intent); 부분입니다.

private void register(int id, double latitude, double longitude, float radius, long expiration) {
	 
	        Intent proximityIntent = new Intent(intentKey);
	        
	        proximityIntent.putExtra("id", id);
	        proximityIntent.putExtra("latitude", latitude);
	        proximityIntent.putExtra("longitude", longitude);
	        PendingIntent intent = PendingIntent.getBroadcast(this, id, proximityIntent, PendingIntent.FLAG_CANCEL_CURRENT);

	        mLocationManager.addProximityAlert(35.197007,  129.0859003, 200, -1, intent);

	   
	    }

따로 액티비티로 구현했을때는 아무 문제가 없다가 서비스로 옮겨서 실행하면 아래 로그캣에서 보시는것처럼  저기서 계속 널 포인트 예외가 발생하고 있습니다. 어떻게 방법이 없을까요?

 

-------------------------------------------------------------------------------------------------------------------------------------

 
gps_Alert_Service.java

package com.example.splash_plus_gmap;

import java.util.ArrayList;

import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import android.location.LocationManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.os.Vibrator;
import android.util.Log;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class gps_Alert_Service extends Service{


private LocationManager mLocationManager;
private CoffeeIntentReceiver mIntentReceiver;

ArrayList mPendingIntentList;

String intentKey = "coffeeProximity";


double get_latitude=0;
double get_longitude=0;



public void onCreate() {
      super.onCreate();
         Log.d("slog", "onCreate()");
    }   

@Override
public IBinder onBind(Intent intent) {

return null;
}




@Override
public int onStartCommand(Intent intent_Latlang, int flags,int startId) {
Log.d("slog", "onStart()");
super.onStart(intent_Latlang, startId); 

get_latitude=intent_Latlang.getExtras().getDouble("art_latitude");
get_longitude=intent_Latlang.getExtras().getDouble("art_longitude");      

        
                  
         register(1001, get_latitude,get_longitude, 200, -1);
         Toast.makeText(getApplicationContext(), "등록완료", 1000).show();

    
return START_NOT_STICKY;
   }

@Override
public void onDestroy() { 
super.onDestroy();
Log.d("slog", "onDestroy()");

unregister();
Toast.makeText(getApplicationContext(), "서비스종료", 1000).show();
}
private void unregister() {
        if (mPendingIntentList != null) {
         for (int i = 0; i < mPendingIntentList.size(); i++) {
         PendingIntent curIntent = (PendingIntent) mPendingIntentList.get(i);
         mLocationManager.removeProximityAlert(curIntent);
         mPendingIntentList.remove(i);
         }
        }

        if (mIntentReceiver != null) {
            unregisterReceiver(mIntentReceiver);
            mIntentReceiver = null;
        }
    }
 private class CoffeeIntentReceiver extends BroadcastReceiver {

       private String mExpectedAction;
       private Intent mLastReceivedIntent;

       public CoffeeIntentReceiver(String expectedAction) {
           mExpectedAction = expectedAction;
           mLastReceivedIntent = null;
       }

       public IntentFilter getFilter() {
           IntentFilter filter = new IntentFilter(mExpectedAction);
           return filter;
       }

       /**
        * 받았을 때 호출되는 메소드
        * 
        * @param context
        * @param intent
        */
       public void onReceive(Context context, Intent intent) {
           if (intent != null) {
             Toast.makeText(context, "접근을 확인하엿습니다.", 200).show();
           
           }
       }

       public Intent getLastReceivedIntent() {
           return mLastReceivedIntent;
       }

       public void clearReceivedIntents() {
           mLastReceivedIntent = null;
       }
   }
 private void register(int id, double latitude, double longitude, float radius, long expiration) {

       Intent proximityIntent = new Intent(intentKey);
       
       proximityIntent.putExtra("id", id);
       proximityIntent.putExtra("latitude", latitude);
       proximityIntent.putExtra("longitude", longitude);
       PendingIntent intent = PendingIntent.getBroadcast(this, id, proximityIntent, PendingIntent.FLAG_CANCEL_CURRENT);

       mLocationManager.addProximityAlert(35.197007,  129.0859003, 200, -1, intent);

  
   }
}

 

--------------------------------------------------------------------------------------------------------------------------------------------

질문을 종료한 이유: 하핫..제가 스스로 답을 찾았네요. 리시버를 등록하는걸 빼먹었습니다(...)
Madobe (130 포인트) 님이 2014년 7월 17일 질문
Madobe님이 2014년 7월 17일 reshown
...