일단 제가 하고싶은 것은 백그라운드로 사용자 위치를 주기적으로 파악하고 그때마다 여러가지 작업을 하는 것입니다. 그래서 IntentService를 상속받아 작업하면 좋겠다고 생각했습니다.
소스코드
public class ProximityAlertService extends IntentService{
LocationManager locationManager;
LocationListener locationListener;
ProximityAlertBroadcastReceiver receiver;
public ProximityAlertService(){
super("ProximityAlertService");
setIntentRedelivery(true);
}
@Override
protected void onHandleIntent(Intent intent) {
Log.i("test","onHandleIntent");
locationManager=(LocationManager)getSystemService(LOCATION_SERVICE);
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);
//gps정보를 디비에서 불러와서 매개변수로 넣어줌(반복문)
locationManager.addProximityAlert(0,0,1000,-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
Handler mHandler = new Handler(getMainLooper());
mHandler.post(new Runnable() {
@Override
public void run() {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);
}
});
}
else{
Log.i("test","onHandleIntent->else");
Log.i("test","네트워크 설정 문제");
}
}
}
처음에는 저 빨간부분의 코드내용을 입력하지 않고 컴파일을 했는데 오류가 나더군요
핸들러 어쩌구저쩌구 하던데 구글링을 해보니까 인텐트서비스는 onHandleIntent함수가 리턴하면 그 즉시 작업스레드가 죽는다고 하더군요 그래서 이 작업스레드에 리스너를 등록하면 안된다고...(제대로 본건지 잘 모르겠지만..)
그래서 핸드러 객체로 메인스레드에 작업을 넘겨줬습니다..저럴 경우 오류없이 잘 돌아가지만...제가 의도한 것이 아닙니다.. 백그라운드에서 작업이 이루어져야하는데...이런 경우 어떻게 하면 좋을까요? 고수님들 도움좀 부탁드립니다.~