Google map marker update from sms receiver transmitter

I am new to Android and java. I am trying to make an application to perform the following task.

  • receive incoming sms (which will have information about latitude and longitude)
  • show them on the map with markers. Therefore, every time an sms card arrives, the card should receive a new marker.

I currently have a map where I can show the point, and I have implemented a broadcast receiver to receive an SMS message with wavelength and longitude.

But I'm not sure how to update the map from the broadcast receiver when receiving new SMS.

Any help or advice would be helpful.

thank

+3
source share
2 answers

, , BroadcastReceiver? ( ) , Activity BroadcastReceiver, , SMS / . , .

+4

3 , :

. SMS BroadcastReceiver

. MapView ItemizedOverlay

. BroadcastReceiver , .

A: SMS

  • BroadcastReceiver:

    public class SMSBroadcastReceiver extends BroadcastReceiver
    {
        private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    
        @Override 
        public void onReceive(Context context, Intent intent) 
        {
            if (intent.getAction().equals (SMS_RECEIVED)) 
            {
                Bundle bundle = intent.getExtras();
                if (bundle != null) 
                {
                    Object[] pdusData = (Object[])bundle.get("pdus");
                    for (int i = 0; i < pdus.length; i++) 
                    {
                        SmsMessage message = SmsMessage.createFromPdu((byte[])pdus[i]);
    
                        /* ... extract lat/long from SMS here */
                    }
                }
            }
        }
    }
    
  • :

    <manifest ... > 
            <application ... >
                    <receiver 
                            android:name=".SMSBroadcastReceiver"
                            android:enabled="true"
                            android:exported="true">
                            <intent-filter>
                                    <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
                            </intent-filter>
                    </receiver>
            </application>
    </manifest>
    

( : Android - SMS-)

B:

  • , ItemizedOverlay, MapView , :

    class LocationOverlay extends ItemizedOverlay<OverlayItem>
    {
            public LocationOverlay(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 */
    
                    GeoPoint point = /* ... the long/lat from the sms */
                    return new OverlayItem(point, null, null);     
            }
    
       @Override 
       public int size() 
       {
                    /* Return the number of markers here */
                    return 1; // You only have one point to display
       } 
    }
    
  • , :

    public class CustomMapActivity extends MapActivity 
    {     
        MapView map;
        @Override
    
            public void onCreate(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 */
                LocationOverlay overlay = new LocationOverlay(getResources().getDrawable(R.drawable.icon));
                map.getOverlays().add(overlay);
            }
    }
    

C:

(. BroadcastReceiver). MapActivity , . MapActivity , -, .

  • ( CustomMapActivity):

    private final String UPDATE_MAP = "com.myco.myapp.UPDATE_MAP"
    
  • BroadcastReceiver ( CustomMapActivity):

    private  BroadcastReceiver updateReceiver =  new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            // custom fields where the marker location is stored
            int longitude = intent.getIntExtra("long");
            int latitude = 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();
        }
    }
    
  • BroadcastReceiver ( CustomMapActivity.onCreate):

    IntentFilter filter = new IntentFilter();
    filter.addAction(UPDATE_MAP);
    registerReceiver(updateReceiver /* from step 2 */, filter);
    
  • BroadcastReceiver ( SMSBroadcastReceiver.onReceive):

    Intent updateIntent = new Intent();
    updateIntent.setAction(UPDATE_MAP);
    updateIntent.putExtra("long", longitude);
    updateIntent.putExtra("lat", latitude);
    context.sendBroadcast(updateIntent);
    
+5

All Articles