Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

send data from service to activity class

// add this function to your service class
 private static void sendMessageToActivity(Location l, String msg) {
        Intent intent = new Intent("GPSLocationUpdates");
        // You can also include some extra data.
        intent.putExtra("Status", msg);
        Bundle b = new Bundle();
        b.putParcelable("Location", l);
        intent.putExtra("Location", b);
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
    }
// call this function in class service
sendMessageToActivity(location, "location");
// add the following line in activity which you want to receive data

LocalBroadcastManager.getInstance(context).registerReceiver(
                mMessageReceiver, new IntentFilter("GPSLocationUpdates"));
// define the mMessageReceiver :
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Get extra data included in the Intent
            String message = intent.getStringExtra("Status");
            Bundle b = intent.getBundleExtra("Location");
            Location lastKnownLoc = (Location) b.getParcelable("Location");
            if (lastKnownLoc != null) {
                latitude=lastKnownLoc.getLatitude();
                longitude=lastKnownLoc.getLongitude();
                Log.e("TAG", "Fragments Maps: "+ lastKnownLoc.getLatitude() + "
 longitude="+lastKnownLoc.getLongitude() );
            }
        }
    };
    // thats all
Comment

PREVIOUS NEXT
Code Example
Java :: java timer 
Java :: check if list includes value java 
Java :: Java How to use Set? 
Java :: jbutton open jframe java 
Java :: Java NoClassDefFoundError but class is there 
Java :: param query spring boot 
Java :: count occurrences of character in string java using hashmap 
Java :: java coalesce 
Java :: Using enum values as string literals 
Java :: java char array to string 
Java :: java throw keyword 
Java :: mockito mock void methods 
Java :: java protected private public 
Java :: list to map of list java 8 
Java :: java repository sql find not in list 
Java :: upper en java 
Java :: Could not determine java version from 
Java :: java standard exception 
Java :: linkedhashmap in java 
Java :: jsonArray to list in java 
Java :: install java using cmd 
Java :: array contain java 
Java :: System.out.println("Hello world") 
Java :: java array sortieren 
Java :: get key from value hashmap 
Java :: android setTextColor not working 
Java :: word count in a string using HashMap Collection 
Java :: java list get first element 
Java :: java long 
Java :: Java print() and println() 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =