2009-09-23 23 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

関連する問題