Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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
 
PREVIOUS NEXT
Tagged: #send #data #service #activity #class
ADD COMMENT
Topic
Name
6+8 =