我有一个在单独的类中接收 SMS 的处理程序。执行处理程序时如何使其更改视图元素?
这是我的代码:
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();
}
}
}
这是 SMS Receiver 类:
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;
}
}
我想修改类MapView
里面SmsReceiver
。
谢谢你。