I have a handler for receiving SMS in a seperate class. How can I make it change the view elements when the handler is executed?
Here is my code:
public class MainActivity extends MapActivity {
MapView mapView;
MapController mc;
GeoPoint p;
private int carNumber;
private String langt;
private String longt;
CheckedTextView text;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SmsReceiver smsreceiver = new SmsReceiver();
draw();
}
public void draw() {
mapView = (MapView) findViewById(R.id.mapView);
LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom);
@SuppressWarnings("deprecation")
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mc = mapView.getController();
double lat = Double.parseDouble("31.977185185185185");
double lng = Double.parseDouble("35.205925925925925");
p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
}
}
Here is the SMS Receiver class:
public class SmsReceiver extends BroadcastReceiver {
MapView mapView;
MapController mc;
GeoPoint p;
private int carNumber;
private String langt;
private String longt;
CheckedTextView text;
public SmsReceiver(MapView mapView) {
this.mapView = mapView;
}
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
@SuppressLint("NewApi") @Override
public void onReceive(final Context context, final Intent intent) {
if (intent != null && SMS_RECEIVED.equals(intent.getAction())) {
final SmsMessage smsMessage = extractSmsMessage(intent);
processMessage(context, smsMessage);
}
}
@SuppressLint("NewApi") private SmsMessage extractSmsMessage(final Intent intent) {
final Bundle pudsBundle = intent.getExtras();
final Object[] pdus = (Object[]) pudsBundle.get("pdus");
final SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[0]);
return smsMessage;
}
}
I want to modify the MapView inside the SmsReceiver class.
Thank you.
Define an interface and use a callback to let the activity know that an SMS has been received.
In your SMS class
When you receive an SMS
In your Activity:
All from memory so please excuse typos and you should improve the SMS class by adding removeSmsReceivedistener and checking that you do not add the same listener twice in setSmsReceivedistener.
Note. Because you use an interface, any class (not just an Activity) can implement it so you can update anywhere in your app. The smsReceiver class doesn’t know or care. It just calls the listeners, if any are registered.