gpt4 book ai didi

android - 如何在 Android 中操作另一个类的 View ?

转载 作者:搜寻专家 更新时间:2023-11-01 09:02:05 24 4
gpt4 key购买 nike

我有一个用于在单独的类中接收 SMS 的处理程序。如何让它在执行处理程序时更改 View 元素?

这是我的代码:

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

这是短信接收器类:

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

我想修改 SmsReceiver 类中的 MapView

谢谢。

最佳答案

定义一个接口(interface)并使用回调让 Activity 知道已收到短信。

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

在您的 Activity 中:

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

注意。因为您使用接口(interface),所以任何类(不仅仅是 Activity)都可以实现它,因此您可以在应用程序的任何位置进行更新。 smsReceiver 类不知道也不关心。它只会调用监听器(如果已注册)。

关于android - 如何在 Android 中操作另一个类的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14388498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com