Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending data from service to activity

Tags:

java

android

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.

like image 575
Sam97305421562 Avatar asked Sep 23 '09 09:09

Sam97305421562


People also ask

How do you communicate between service and activity?

We already know that we can communicate with Service from activity just by using method startService() and passing Intent to the argument in the method, or either we can use bindService() to bind the service to the activity with argument Intent.

How do you send data to activities?

The easiest way to do this would be to pass the session id to the signout activity in the Intent you're using to start the activity: Intent intent = new Intent(getBaseContext(), SignoutActivity. class); intent. putExtra("EXTRA_SESSION_ID", sessionId); startActivity(intent);

How pass data from service to activity in Android Studio?

class MainActivity : AppCompatActivity() { private val sb = object :SerToBroad(){ override fun broadcastResult(connected: String?) { t(connected) } } override fun onCreate(savedInstanceState: Bundle?) { super. onCreate(savedInstanceState) setContentView(R. layout. activity_main) } fun t(connected: String?) { Toast.

How do I send a callback from service to activity?

You need to create a BroadcastReceiver in your activity (be sure to register it in onResume() and unregister it in onPause() ) and notify it via a broadcast, providing an Intent .


1 Answers

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>
like image 112
Niko Gamulin Avatar answered Oct 05 '22 03:10

Niko Gamulin