2013-02-13 6 views
0

내 응용 프로그램에 서비스를 만들고 있습니다. 여기에 제가 사용한 코드가 있습니다.Android 서비스가 시작되지 않았습니다.

public class LocationService extends Service{ 

@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public void onStart(Intent intent, int startId) { 
    super.onStart(intent, startId); 
    Log.e("Location Service", "onStrat of service"); 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
    Log.e("Location Service", "onCreate of service"); 
    LocationOperations locationOperations = new LocationOperations(getBaseContext()); 
    locationOperations.retrieveLocation(); 
} 

}

는 또한 활동

Context context = this; 
    Intent service = new Intent(context, LocationService.class); 
    service.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startService(service); 

의에서 onCreate 방법에서이 서비스를 호출하는 applicatoin 태그

<service android:name=".LocationService" 
     android:process=":LocationService"> 
    </service> 

내에서 매니페스트 파일에 정의하지만이 서비스가 아닌 LogCat 창에서 로그 메시지를 볼 수 없습니다. 도와주세요.

+0

http://stackoverflow.com/questions/4759676/why-is-this-simple-service-not-starting – Nermeen

+0

왜 서비스를 별도의 프로세스로 만들고 있습니까? –

+0

Beacause GPS, WIFI 또는 셀 타워를 통해 위치 좌표를 얻으려면 응용 프로그램이 닫힐 때까지 계속해서 사용할 수 있어야합니다. 그래서 나는 백그라운드 스레드에서 실행 싶었어요. –

답변

0
 ServiceConnection serviceConnection = new ServiceConnection() { 

       @Override 
       public void onServiceDisconnected(ComponentName name) { 
           //do stuff when service disconnected 
       } 

       @Override 
       public void onServiceConnected(ComponentName name, IBinder service) { 
           //do stuff when service connected 
       } 
      }; 


context.bindService(new Intent(context, LocationService.class), serviceConnection , 
        Context.BIND_AUTO_CREATE); 

희망이 도움

0

ONSTART()가 대신 OnStartCommand()를 오버라이드 (override), 사용되지 않는, 즉 함수가 당신이 만약 startActivity를 호출 할 때()가 호출 이잖아해야한다.

또한 왜 Intent.FLAG_ACTIVITY_NEW_TASK 플래그를 의도에 추가했는지 확실하지 않습니다. 나는 그 필요성을 여기에서 생각하지 않는다.

관련 문제