일단 소스입니다.
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가 시스템이 방송을 보낼때가되면 알아서 보내는 식으로 알고 있습니다..그래서 방송을 한번했을떄의 시점을 잘 모르겠습니다..
어떻게 해야할까요 선배님들 ㅠ