我有一个处理程序,用于在单独的类中接收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
。
谢谢。
答案 0 :(得分:2)
定义一个接口并使用回调让活动知道已收到短信。
public Interface SmsReceivedListener {
void onSmsReceived(int arg1, string arg2); ..<----add arguments you want to pass back
}
在您的短信课程中
ArrayList<SmsReceivedistener> listeners = new ArrayList<SmsReceivedistener>();
...
public void setSmsReceivedistener(SmsReceivedistenerlistener){
listeners.add(listener);
}
收到短信时
for (SmsReceivedistener listener:listeners){
listener.onSmsReceived(arg1, arg2);
}
在您的活动中:
public class MainActivity extends MapActivity implements SmsReceivedListener {
...
@Override
public void onCreate(Bundle savedInstanceState)
{
...
smsReceiver.setSmsReceivedistener(this);
...
}
public void onSmsReceived(int arg1, string arg2){
// do whatever you need to do
}
全部来自内存所以请原谅错别字,你应该通过添加removeSmsReceivedistener并检查你没有在setSmsReceivedistener中添加两次相同的监听器来改进SMS类。
请注意。因为您使用的是界面,所以任何类(不仅仅是一个Activity)都可以实现它,以便您可以在应用中的任何位置进行更新。 smsReceiver类不知道或不关心。它只是调用监听器,如果有人注册的话。
答案 1 :(得分:0)
将此添加到onReceive()方法:
if (context instanceof MainActivity) {
//modify the map
((MainActivity)context).findViewById(...);
}
答案 2 :(得分:0)
试试这个
[1] "End proc. : C:\\TimeSeries_RiskFactors\\20171119\\C0.GBMRISK.1100000156062.txt"
[1] "End proc. : C:\\TimeSeries_RiskFactors\\20171119\\C0.GBMRISK.1100000156063.txt"
[1] "End proc. : C:\\TimeSeries_RiskFactors\\20171220\\C0.GBMRISK.1100000156064.txt"
Show Traceback
Rerun with Debug
Error in read.table(name, header = T, sep = "@", dec = ",", comment.char = "") :
duplicate 'row.names' are not allowed
[1]"End proc. : C:\\TimeSeries_RiskFactors\\20171220\\C0.GBMRISK.1100000156065.txt"
在上面的示例中,视图bt1来自另一个类。您可以使用类实例访问bt1并获取id.Now,执行您想要的bt1实例。
注意:确保bt1在另一个类中是公开的