2009-09-23 4 views
13

서비스에서 알림을 통해 활동으로 데이터를 보낼 때 문제가 발생합니다. 알림을 클릭합니다. 활동이 호출되지만 묶음을 통해 일부 매개 변수를 추가하려고하면 매개 변수를 가져올 수 없습니다. 그 의도라고, 나는 링크를 통해 갔다. How to send parameters from a notification-click to an activity?서비스에서 활동으로 데이터 보내기

그래도 운이 없다.

다른 사람에게도 동일한 문제가 발생 했습니까?

미리 감사드립니다.

+0

서비스에서 활동으로 데이터를 보내는 방법은 무엇입니까? –

답변

18

매니페스트 파일도 수정해야합니다.

이러한 변수와 메소드 서비스 클래스의 구성원 인 : 여기

가 작동하는 예입니다

public static final String MOVEMENT_UPDATE = "com.client.gaitlink.AccelerationService.action.MOVEMENT_UPDATE"; 
    public static final String ACCELERATION_X = "com.client.gaitlink.AccelerationService.ACCELERATION_X"; 
    public static final String ACCELERATION_Y = "com.client.gaitlink.AccelerationService.ACCELERATION_Y"; 
    public static final String ACCELERATION_Z = "com.client.gaitlink.AccelerationService.ACCELERATION_Z"; 

private void announceAccelerationChanges()//this method sends broadcast messages 
    { 
     Intent intent = new Intent(MOVEMENT_UPDATE); 
     intent.putExtra(ACCELERATION_X, accelerationX); 
     intent.putExtra(ACCELERATION_Y, accelerationY); 
     intent.putExtra(ACCELERATION_Z, accelerationZ); 

     sendBroadcast(intent); 
    } 

그리고 이것은 주요 활동의 방법이 있습니다 : 당신은 수신기를 등록해야

onResume 메소드에서 :

@Override 
    public void onResume() 
    { 

     IntentFilter movementFilter; 
     movementFilter = new IntentFilter(AccelerationService.MOVEMENT_UPDATE); 
     accelerationReceiver = new AccelerationServiceReceiver(); 
     registerReceiver(accelerationReceiver, movementFilter); 


     startAccelerationService(); 

     super.onResume(); 
    } 

    private void startAccelerationService() 
    { 
     startService(new Intent(this, AccelerationService.class)); 
    } 

    public class AccelerationServiceReceiver extends BroadcastReceiver 
    { 
     @Override 
     public void onReceive(Context context, Intent intent)//this method receives broadcast messages. Be sure to modify AndroidManifest.xml file in order to enable message receiving 
     { 
      accelerationX = intent.getDoubleExtra(AccelerationService.ACCELERATION_X, 0); 
      accelerationY = intent.getDoubleExtra(AccelerationService.ACCELERATION_Y, 0); 
      accelerationZ = intent.getDoubleExtra(AccelerationService.ACCELERATION_Z, 0); 

      announceSession(); 

      updateGUI(); 
     } 
    } 

이것은 t입니다. 그는 브로드 캐스트 메시지를 수신하기 위해 설정해야하는 AndroidManifest.xml 파일의 일부입니다.

<activity android:name=".GaitLink" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 

       <action android:name="com.client.gaitlink.CommunicationService.action.ACTIVITY_STATUS_UPDATE" /> 

      </intent-filter> 
     </activity> 
+0

나는 이것을 시도했지만 예상대로 작동하지 않습니다. Log 서술문을 작성했지만 onRecieve 메소드에 표시하지 않습니다. 내가 말한 것과 같은 일을하고있을 수 있습니다! – Sam97305421562

+0

서비스가 메시지를 브로드 캐스트합니까? 활동에 BroadcastReceiver가 시작되어 실행 되었습니까? AndroidManifest.xml에 인 텐트 필터를 추가 했습니까? –

+0

AndroidManifest.xml 파일에 BroadcastReceiver를 추가했습니다. 예, 서비스가 브로드 캐스트를 수행합니다. BroadcastReceiver가 시작되었는지 어떻게 확인할 수 있습니까? – Sam97305421562

관련 문제