从广播短信接收器更新谷歌地图标记

Alo*_*lok 3 android google-maps-api-3 android-intent android-layout mapactivity

我是android和java的新手.我正在尝试使应用程序执行以下任务.

  • 接收传入的短信(具有纬度和经度信息)
  • 用标记在地图上显示它们所以每次短信来到地图时都应该得到一个新的标记.

目前我有一张地图,我可以展示一个点,我已经实现了一个广播接收器,以获得形式的短信和经度.

但我不确定如何在接收新短信时从广播接收器更新地图.

任何帮助或提示都会很有用.

谢谢

Ton*_*ony 5

您需要解决以下3个问题:

A.通过a接收短信 BroadcastReceiver

B. MapView使用an 注释ItemizedOverlay

C.从BroadcastReceiver显示地图的活动传达更新

项目A:接收短信

  1. 实施你的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 */
                    }
                }
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在应用清单中指定您的广播接收器:

    <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>
    
    Run Code Online (Sandbox Code Playgroud)

(积分转到此帖中的海报:Android - 短信广播接收器)

项目B:注释地图

  1. 创建一个派生类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
       } 
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 现在,将叠加层合并到显示实际地图的活动中:

    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);
            }
    }
    
    Run Code Online (Sandbox Code Playgroud)

项目C:传达更新

这是最棘手的部分(另请参阅从BroadcastReceiver更新活动).如果应用程序MapActivity当前可见,则需要通知新接收的标记.如果MapActivity未激活,则任何接收的点都需要存储在某处,直到用户选择查看地图为止.

  1. 定义私有意图(in CustomMapActivity):

    private final String UPDATE_MAP = "com.myco.myapp.UPDATE_MAP"
    
    Run Code Online (Sandbox Code Playgroud)
  2. 创建私有BroadcastReceiver(in 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();
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. BroadcastReceiver在活动开始时注册您的私人(添加到此CustomMapActivity.onCreate):

    IntentFilter filter = new IntentFilter();
    filter.addAction(UPDATE_MAP);
    registerReceiver(updateReceiver /* from step 2 */, filter);
    
    Run Code Online (Sandbox Code Playgroud)
  4. 从公众调用您的私人意图BroadcastReceiver(添加到此SMSBroadcastReceiver.onReceive):

    Intent updateIntent = new Intent();
    updateIntent.setAction(UPDATE_MAP);
    updateIntent.putExtra("long", longitude);
    updateIntent.putExtra("lat", latitude);
    context.sendBroadcast(updateIntent);
    
    Run Code Online (Sandbox Code Playgroud)