从广播SMS接收更新谷歌地图标记标记、地图、SMS

2023-09-06 00:47:15 作者:わがまま(任性)

我是新手到Android和Java。我试图让应用程序执行以下任务。

I am a newbie to android and java. I am trying to make an application to perform following task.

在接收传入的SMS(这将有经度和纬度信息) 在显示他们与标记地图 所以每次短信来映射应该得到一个新的标志物。 receive incoming sms (which will have latitude and longitude information) show them on map with markers So every time an sms comes map should get a new marker.

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

currently I have a map where I can show a point and I have implemented a broadcast receiver to get lattitude and longitude form SMS.

但我不能确定如何更新从广播接收器在地图上收到新的短信。

But I am unsure how to update the map from broadcast receiver on receiving a new sms.

任何帮助或提示将是有益的。

Any help or tips will be useful.

感谢您

推荐答案

有3个项目,你需要解决:

There are 3 items you need to address:

一个。通过接收短信一个的BroadcastReceiver

A. Receive an SMS via a BroadcastReceiver

乙。批注图形页面使用 ItemizedOverlay

℃。从的BroadcastReceiver 沟通的更新显示在地图的活动

C. Communicate the updates from the BroadcastReceiver to the activity showing the map

实现你的的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 */
                }
            }
        }
    }
}

指定的应用程序清单的广播接收器:

Specify your broadcast receiver in the app manifest:

<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>

(学分转到本主题海报:机器人 - 短信广播接收机)

(Credits go to the posters in this thread: Android - SMS Broadcast receiver)

创建源自 ItemizedOverlay 一类,它是用来告知任何标记物图形页面这需要显示的:

Create a class derived from ItemizedOverlay, which is used to inform a MapView of any markers which need to be displayed:

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
   } 
}

怎样在谷歌地图卫星加一个自己显示标签

现在,将覆盖到一个活动,显示实际的图:

Now, incorporate the overlay into an activity that displays the actual map:

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);
        }
}

这是最棘手的部分(参见Updating从一个BroadcastReceiver 的活动)。如果应用程序的 MapActivity 是当前可见的,它需要新接收的标志的通知。如果 MapActivity 不活跃,任何接收需要被存储的地方,直到用户选择以查看地图点

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.

定义的私人意图(在 CustomMapActivity ):

private final String UPDATE_MAP = "com.myco.myapp.UPDATE_MAP"

创建私有的BroadcastReceiver (在 CustomMapActivity ):

Create the private 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();
    }
}

填写您的私人的BroadcastReceiver 当活动开始(它添加到 CustomMapActivity.onCreate ):

Register your private BroadcastReceiver when the activity is started (add this to CustomMapActivity.onCreate):

IntentFilter filter = new IntentFilter();
filter.addAction(UPDATE_MAP);
registerReceiver(updateReceiver /* from step 2 */, filter);

从调用您的私人意图公的BroadcastReceiver (将其添加到 SMSBroadcastReceiver.onReceive ):

Invoke your private intent from the public BroadcastReceiver (add this to SMSBroadcastReceiver.onReceive):

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