Mr.Goomer
Mr.Goomer

Reputation: 71

Tranfer Data from service to activity

When user kill app,my service is running in the background. It has a button which I can return to the map activity.However,when the user return to the app through the notification button after destroying the app,the map created but the information I am transfering from my service to my map activity is null.

When the user isn't killing the app and return through the notification button,the data exist.

This is my code:

//Map Activity

        //OnPause = transfer the data to service intent(Working fine)

        BackgroundLocation backgroundLocation = new BackgroundLocation();
        mServiceIntent = new Intent(this, backgroundLocation.getClass());

        if (!isMyServiceRunning(backgroundLocation.getClass())) {

            mServiceIntent.putExtra("AddressBackgound",mAddress);
            mServiceIntent.putExtra("AddressLatBackgound",destinationLat);
            mServiceIntent.putExtra("AddressLngBackgound",destinationLng);

            startService(mServiceIntent);
        }


           // OnMapReady = Getting the data from service intent(return null for all data)

            if (myLocation != null) {

                BackgroundLocation backgroundLocation = new BackgroundLocation();
                mServiceIntent = new Intent(this, backgroundLocation.getClass());

                Bundle extras = getIntent().getExtras();

                if (isMyServiceRunning(backgroundLocation.getClass()) && extras != null) {

                    String mAddress2 = extras.getString("AddressBackgound22");
                    Double destinationLat2 = extras.getDouble("AddressLatBackgound22");
                    Double destinationLng2 = extras.getDouble("AddressLngBackgound22");

                    Log.e("onResume", "onResume stats");
                    Log.e("Address", "" + mAddress2);
                    Log.e("Lat", String.valueOf(destinationLat2));
                    Log.e("Lng", String.valueOf(destinationLng2));

                    Log.e("OnMapReady","Service is running....");

                }
                else{

                     Log.e("OnMapReady","Service is not running");

                }
          }

Background Location(Service Intent) = Getting information from MapsActivity and returnthe information to MapsActivity aswell.

//Service Intent 

// OnStartCommand



        Bundle extras = intent.getExtras();

        if (extras != null) {

//getting the data to the service is working fine even when the app killed the service still working with the data.

            mAddress = extras.getString("AddressBackgound");
            destinationLat = extras.getDouble("AddressLatBackgound");
            destinationLng = extras.getDouble("AddressLngBackgound");


            //This is what I am trying to send to MapsActivity:

            extras.putString("AddressBackgound22",mAddress);
            extras.putDouble("AddressLatBackgound22",destinationLat);
            extras.putDouble("AddressLngBackgound22",destinationLng);


            Log.e("onStartCommand", "onStartCommand started");

            Log.e("Address","" + mAddress);
            Log.e("Lat",  "" + destinationLat);
            Log.e("Lng",  "" + destinationLng);


        }

Thank you for your time.

Upvotes: 0

Views: 59

Answers (3)

Mohammadreza
Mohammadreza

Reputation: 64

There are many way,I say some of them:

1-Use store data in service (such as SharedPrefrences,DB and ...) and retrive in activity

2-Use eventbus or broadcast receivers

3-Use callback patern for callback data from service to activiy

Upvotes: 1

Mr.Goomer
Mr.Goomer

Reputation: 71

So thanks to @Mohammadreza I used sharedPref on the service intent and its worked.

This is what I did:

       public static final String MY_PREFS_NAME = "MyData";

//Save SharedPref(BackgroundActivity):

        SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
        editor.putString("AddressBackgound",mAddress);
        editor.putString("AddressLatBackgound", String.valueOf(destinationLat));
        editor.putString("AddressLngBackgound", String.valueOf(destinationLng));
        editor.apply();

//Read SharedPref(MapsActivity):

            SharedPreferences prefs = getSharedPreferences(BackgroundLocation.MY_PREFS_NAME, MODE_PRIVATE);

BackgroundLocation backgroundLocation = new BackgroundLocation();

            if (isMyServiceRunning(backgroundLocation.getClass()) && prefs != null ) {


                 String mAddress = prefs.getString("AddressBackgound","mAddress");
                 Double UserStartLocationLat = Double.parseDouble(String.valueOf(Double.valueOf(prefs.getString("UserStartLocationLat", "UserStartLocationLatNull"))));
                 Double UserStartLocationLng = Double.parseDouble(String.valueOf(Double.valueOf(prefs.getString("UserStartLocationLng", "UserStartLocationLngNull"))));

             }

isMyServiceRunning:

private boolean isMyServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            Log.i ("Service status", "Running");
            return true;
        }
    }
    Log.i ("Service status", "Not running");
    return false;
}

Upvotes: 1

Shahjahan Samoon
Shahjahan Samoon

Reputation: 1

you can use eventbus or broadcast receivers to send data from service to Activity/Fragment

Upvotes: 0

Related Questions