13

I am having issue when sending data from Service to Activity through Notification , I click a Notification an Activity get invoked but when i try to add some parameters through bundle i am not able to get the parameters in that called intent , I have gone through the link How to send parameters from a notification-click to an activity?

But still no luck.

Has the same issue occurred with somebody else ?

Thanks in advance.

Community
  • 1
  • 1
Sam97305421562
  • 3,027
  • 10
  • 35
  • 45

1 Answers1

18

You have to modify the Manifest file as well.

Here is the example that works:

These variables and methods are members of Service class:

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);
    }

And this are the methods from Main activity:

You have to register receiver in the onResume method:

    @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();
        }
    }

This is the part of AndroidManifest.xml file that has to be set in order to receive broadcast messages:

<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>
Niko Gamulin
  • 66,025
  • 95
  • 221
  • 286
  • I have tried this but it is not working as expected .. i have written Log statements but is is not displaying in onRecieve method .what wrong i may be doing i am doing the same as said! – Sam97305421562 Sep 24 '09 at 09:42
  • Does the service broadcast the message? Has the BroadcastReceiver been started and running in the activity? Have you added intent filter in AndroidManifest.xml? – Niko Gamulin Sep 24 '09 at 10:39
  • i have added BroadcastReceiver in AndroidManifest.xml file , yes the service does the broadcast how can i confirm that the BroadcastReceiver has been started. – Sam97305421562 Sep 24 '09 at 11:41
  • Niko, your example is working very well. It was helped me a lot. Thank you very much. – fyasar Jan 18 '11 at 23:53
  • Your code doesn't 100% work, but using broadcasts and BroadcastReceivers was exactly what I wanted to use – CVertex Apr 22 '11 at 13:47
  • For completeness you may want to add a notice about unregistering the broadcast in the onPause method. – Grzegorz Adam Hankiewicz Feb 19 '12 at 22:27
  • +1 Wow, finally a good and "correct" solution for communication between `Service` and `Activity`. Thank you! – Zelphir Kaltstahl Jan 28 '16 at 21:02