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

ProximityAlert에서 방송수신을 한번만 하려면 어떻게 해야하나요?

0 추천

 

일단 소스입니다.

package com.example.ab.location9;
 
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
 
/**
 * Created by AB on 2015-03-23.
 */
public class ProximityAlertService extends Service {
    LocationManager locationManager;
    LocationListener locationListener;
    ProximityAlertBroadcastReceiver receiver;
    ProximityAlertThread thread;
    @Override
    public void onCreate() {
        super.onCreate();
        locationManager=(LocationManager)getSystemService(LOCATION_SERVICE);
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        thread=new ProximityAlertThread();
        thread.start();
        return Service.START_STICKY;
    }
 
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        locationManager.removeUpdates(locationListener);
        unregisterReceiver(receiver);
    }
 
    public class ProximityAlertThread extends Thread {
 
        @Override
        public void run() {
            super.run();
            Looper.prepare();
            locationListener=new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    Log.i("test", "onLocationChanged");
                    receiver=new ProximityAlertBroadcastReceiver();
                    IntentFilter filter=new IntentFilter("com.example.ab.location7");
                    registerReceiver(receiver,filter);
 
                    Intent intent=new Intent("com.example.ab.location7");
                    PendingIntent proximityIntent=PendingIntent.getBroadcast(getApplicationContext(),0,intent,0);
                    
                    locationManager.addProximityAlert(37.58192502,127.0109278,5000,-1,proximityIntent);
                }
 
                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {
 
                }
 
                @Override
                public void onProviderEnabled(String provider) {
 
                }
 
                @Override
                public void onProviderDisabled(String provider) {
 
                }
            };
            if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
                Log.i("test","onHandleIntent->if");
                //매개변수 1000*60*10,1000
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener,Looper.myLooper());
            }
            else{
                Log.i("test","onHandleIntent->else");
                Log.i("test","네트워크 설정 문제");
            }
            Looper.loop();
        }
    }
 
}
 
소스 설명은...서비스에서 스레드를 만들어 스레드를 돌립니다.
스레드에서 사용자 위치가 변할때마다 addProximityAlert를 호출합니다..
물론 지금은 테스트용으로 아무 지피에스 정보를 넣어놨는데 이 정보는 동적으로 변하게 수정할것입니다.
 
ProximityAlert에서 방송수신을 한번만 하려면 어떻게 해야하나요?
unregisterReceiver으로 방송수신을 멈추는 것은 알고 있습니다..
근데 이걸 어디다가..해줘야 할지 모르겠습니다..왜냐면
제가 잘 알고있는지 모르겠지만  PendingIntent.getbroadcast가 시스템이 방송을 보낼때가되면 알아서 보내는 식으로 알고 있습니다..그래서 방송을 한번했을떄의 시점을 잘 모르겠습니다..
어떻게 해야할까요 선배님들 ㅠ
 
빙구짱 (1,720 포인트) 님이 2015년 3월 26일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...