Updating Google Maps Marker From A Broadcast Sms Receiver
Solution 1:
There are 3 items you need to address:
A. Receive an SMS via a BroadcastReceiver
B. Annotating a MapView
using an ItemizedOverlay
C. Communicate the updates from the BroadcastReceiver
to the activity showing the map
Item A: Receive SMS
Implement your
BroadcastReceiver
class:publicclassSMSBroadcastReceiverextendsBroadcastReceiver { privatestaticfinalStringSMS_RECEIVED="android.provider.Telephony.SMS_RECEIVED"; @OverridepublicvoidonReceive(Context context, Intent intent) { if (intent.getAction().equals (SMS_RECEIVED)) { Bundlebundle= intent.getExtras(); if (bundle != null) { Object[] pdusData = (Object[])bundle.get("pdus"); for (inti=0; i < pdus.length; i++) { SmsMessagemessage= SmsMessage.createFromPdu((byte[])pdus[i]); /* ... extract lat/long from SMS here */ } } } } }
Specify your broadcast receiver in the app manifest:
<manifest... ><application... ><receiverandroid:name=".SMSBroadcastReceiver"android:enabled="true"android:exported="true"><intent-filter><actionandroid:name="android.provider.Telephony.SMS_RECEIVED"></action></intent-filter></receiver></application></manifest>
(Credits go to the posters in this thread: Android - SMS Broadcast receiver)
Item B: Annotate the Map
Create a class derived from
ItemizedOverlay
, which is used to inform aMapView
of any markers which need to be displayed:classLocationOverlayextendsItemizedOverlay<OverlayItem> { publicLocationOverlay(Drawable marker) { /* Initialize this class with a suitable `Drawable` to use as a marker image */super( boundCenterBottom(marker)); } @Override protected OverlayItem createItem(int itemNumber) { /* This method is called to query each overlay item. Change this method if you have more than one marker */GeoPointpoint=/* ... the long/lat from the sms */returnnewOverlayItem(point, null, null); } @Overridepublicintsize() { /* Return the number of markers here */return1; // You only have one point to display } }
Now, incorporate the overlay into an activity that displays the actual map:
publicclassCustomMapActivityextendsMapActivity { MapView map; @OverridepublicvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /* We're assuming you've set up your map as a resource */ map = (MapView)findViewById(R.id.map); /* We'll create the custom ItemizedOverlay and add it to the map */LocationOverlayoverlay=newLocationOverlay(getResources().getDrawable(R.drawable.icon)); map.getOverlays().add(overlay); } }
Item C: Communicate the updates
This is the trickiest part (see also Updating an Activity from a BroadcastReceiver). If the app's MapActivity
is currently visible, it needs to be notified of the newly received markers. If the MapActivity
isn't active, any received points need to be stored somewhere until the user chooses to view the map.
Define the private intent (in
CustomMapActivity
):privatefinalStringUPDATE_MAP="com.myco.myapp.UPDATE_MAP"
Create the private
BroadcastReceiver
(inCustomMapActivity
):privateBroadcastReceiverupdateReceiver=newBroadcastReceiver() { @OverridepublicvoidonReceive(Context context, Intent intent) { // custom fields where the marker location is storedintlongitude= intent.getIntExtra("long"); intlatitude= intent.getIntExtra("lat"); // ... add the point to the `LocationOverlay` ...// (You will need to modify `LocationOverlay` if you wish to track more// than one location)// Refresh the map map.invalidate(); } }
Register your private
BroadcastReceiver
when the activity is started (add this toCustomMapActivity.onCreate
):IntentFilter filter = new IntentFilter(); filter.addAction(UPDATE_MAP); registerReceiver(updateReceiver /* from step 2 */, filter);
Invoke your private intent from the public
BroadcastReceiver
(add this toSMSBroadcastReceiver.onReceive
):IntentupdateIntent=newIntent(); updateIntent.setAction(UPDATE_MAP); updateIntent.putExtra("long", longitude); updateIntent.putExtra("lat", latitude); context.sendBroadcast(updateIntent);
Solution 2:
It sounds like you are looking for detail on how to communicate between your Activity and the BroadcastReceiver? One approach (there are many different approaches) is to have your map Activity register a temporary BroadcastReceiver that is setup to only listen to private broadcasts from your app, and then have your SMS broadcast receiver generate a new broadcast with the latitude/longitude from the SMS. In your map Activity your receiver will add a new point to the map every time it receives a new private broadcast.
Post a Comment for "Updating Google Maps Marker From A Broadcast Sms Receiver"