Last active
February 27, 2018 05:47
-
-
Save HRankit/857c4fc57fae1fc2abff7ee99d33580b to your computer and use it in GitHub Desktop.
Service to Activity Data Transfer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In onCreate | |
LocalBroadcastManager.getInstance(getActivity()).registerReceiver( | |
MyMsgReceiver, new IntentFilter("GPSLocation")); | |
// End onCreate | |
private BroadcastReceiver MyMsgReceiver = 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"); | |
lastKnownLoc = (Location) b.getParcelable("Location"); | |
if (lastKnownLoc != null) { | |
tvLatitude.setText(String.valueOf(lastKnownLoc.getLatitude())); | |
tvLongitude | |
.setText(String.valueOf(lastKnownLoc.getLongitude())); | |
tvAccuracy.setText(String.valueOf(lastKnownLoc.getAccuracy())); | |
tvTimestamp.setText((new Date(lastKnownLoc.getTime()) | |
.toString())); | |
tvProvider.setText(lastKnownLoc.getProvider()); | |
} | |
tvStatus.setText(message); | |
// Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); | |
} | |
}; | |
// in onPause | |
unregisterReceiver(MyMsgReceiver ); | |
// End onPause |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void sendMessageToActivity(Location l, String msg) { | |
Intent intent = new Intent("GPSLocation"); | |
// 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment