1

위치 업데이트를 받고있는 서비스가 있습니다. 서비스가 위치를 성공적으로 반환합니다. 그러나 그 후에는 듣고있는 활동에 위치를 방송하려고합니다. 내 활동에 수신기를 등록했지만 어떤 이유로 onReceive 메서드가 호출되지 않습니다. 내 서비스 내의 내 onLocationChanged 메소드 내부의 코드는 다음과 같습니다. 내에서 onCreate 방법 내 활동 내부 BroadcastReceiver onReceive 메서드가 호출되지 않았습니까?

@Override 
public void onLocationChanged(Location location) { 
    Intent intent = new Intent(); 
    intent.setAction("LocationBroadcast"); 
    double lat = location.getLatitude(); 
    double lng = location.getLongitude(); 
    intent.putExtra("lat", lat); 
    intent.putExtra("lng", lng); 
    //I am initializing the broadcaster object in onCreate method of my service but I am putting it here for simplicity 
    broadcaster = LocalBroadcastManager.getInstance(this); 
    //This Toast successfully shows my coordinates so I know the problem is not with this method 
    Toast.makeText(GoogleFusedLocationApiService.this, ""+lat+", "+lng+"", Toast.LENGTH_SHORT).show(); 
    broadcaster.sendBroadcast(intent); 
} 

, 정말 같은 LocationBroadcast에 등록하고있다.

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    ... 
    ... 
    IntentFilter intentFilter = new IntentFilter("LocationBroadcast"); 
    super.registerReceiver(mMessageReceiver, intentFilter); 
    startService(new Intent(MyApp.getAppContext(), GoogleFusedLocationApiService.class)); 
} 

나는 this.registerReceiver(mMessageReceiver, intentFilter);LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, intentFilter);을 시도했지만 어느 쪽도했다.

여기에, 또한

public BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     double lat = intent.getDoubleExtra("lat", 0); 
     double lng = intent.getDoubleExtra("lng", 0); 
     // This Toast never shows and neither can I debug this method at all 
     // so for now the only conclusion is the broadcast is not being received 
     Toast.makeText(MyApp.getAppContext(), "Cordinates are "+lat+", "+lng+"", Toast.LENGTH_SHORT).show(); 
    } 
}; 

, 나는 몇 가지 조사 후 문제가 생각할 수있는 몇 가지 세부 사항 정의 내 mMessageReceiver입니다. 나는 manifestreceiver을 선언하지 않았습니다. 왜냐하면 브로드 캐스트를 받았을 때 응용 프로그램을 시작하려고 할 때만 수행한다는 것을 알았 기 때문에 이미 실행 중일 때 내 응용 프로그램이 반응하기를 원합니다. 서비스가 브로드 캐스트를 보낼 때마다 실행되지 않습니다.

그리고 활동이 이미 충돌하지 않는 FragmentActivity

앱으로 확장되고 onLocationChanged가 이렇게 등록 된 브로드 캐스트 리시버 후에 시작되고있는 서비스 내부에 위치한 하나 때문에 BroadcastReceiver로 활동을 확장하지 않은 onLocationChanged BroadcastReceiver가 등록되기 전에 호출되지 않습니다.

+0

수신기 위치는 정확히 어디에 있습니까? onlocationChanged 또는 다른 활동과 동일한 활동에서? –

+0

'onLocationChanged'는 별도의 서비스에 있습니다. 수신자는 예를 들어 방송 당시 볼 수있는 활동 안에 있습니다. –

+0

왜 super.registerReceiver를 부르시겠습니까? – JoxTraex

답변

0

나는이 문제를 해결할 수있었습니다. 앞으로이 문제에 직면 한 다른 사람들을 위해 내 연구 결과와 답변을 게시 할 것입니다. 나는 아마도 여기에 많은 개념들을 어리석게 생각했을 것이다. 그러나이 특정한 문제를 이해하기 위해서 나는 나의 최선의 이해에 정확하도록 노력할 것이다.

답은

문제는 내가 방송을 전송하고 같은 Context으로 방송을 수신하지 않은 것이 었습니다. 내가 그 뜻은이 내가 android:process 속성이 서비스는 응용 프로그램이 실행되는 과정에서 다른 프로세스에서 실행되는 것을 의미했다 Manifest 파일

<service 
     android:name=".GoogleFusedLocationApiService" 
     android:process=":google_fused_location_api_service"/> 

내 서비스를 선언 한 방법이다입니다. 그래서 제가 호출 할 때 super.registerReceiver(mMessageReceiver, intentFilter);이 Activity의 컨텍스트에서 호출되고 있었고 broadcaster = LocalBroadcastManager.getInstance(this); broadcaster.sendBroadcast(intent);을 여기 sendBroadcast이라고 부르면 다른 프로세스가 실행되는 서비스의 컨텍스트에서 호출되고있었습니다. 그래서 나는 다른 컨텍스트에서 수신기를 등록하고 다른 컨텍스트에서 브로드 캐스트를 보내는 것을 봅니다.

LocalBroadcastManager은 동일한 프로세스, 즉 컨텍스트에서 브로드 캐스트를 보내고받을 때만 작동합니다. 따라서이 경우 내 서비스와 앱/액티비티가 실행 중이거나 별도의 프로세스 즉 문맥이므로 LocalBroadcastManager을 사용할 수 없습니다. 글로벌 방송을 사용해야하고 방송을 등록하고 동일한 컨텍스트에서 방송을 보내고 있는지 확인해야합니다.

이제부터 난 당신이 내가 방송 수신기를 등록하면 지금 this answer에서 할 수단이 컨텍스트를 사용하여 해당 방송을 전송하는 방법을 배울 수 있습니다 단순히, MyApp.getAppContext()를 호출하여 어디서든 사용할 수있는 응용 프로그램의 정적 컨텍스트를 가지고있는 둘 다 MyApp.getAppContext() 인 동일한 컨텍스트에서 수행되며 이제는 성공적으로 브로드 캐스트를 수신하기 시작합니다. 그래서

당신이 당신의 서비스에 대한 별도의 프로세스가 있다면 당신은 당신의 서비스에 대해 동일한 프로세스를하거나 매니페스트에서 서비스 태그의 android:process 속성이없는 경우, MyApp.getAppContext().registerReceiver()MyApp.getAppContext().sendBroadcast()

를 사용 정리해하기 파일을 사용하는 경우 LocalBroadcastManager.getInstance(this).registerReceiver()LocalBroadcastManager.getInstance(this).sendBroadcast()을 사용할 수 있습니다.

여기에도 MyApp.getAppContext을 사용할 수 있지만 LocalBroadcastManager을 사용하면 두 번째 경우의 작업을 수행하는 올바른 방법입니다.

관련 문제