gpt4 book ai didi

Android:接近警报后的setText

转载 作者:行者123 更新时间:2023-11-30 04:13:15 25 4
gpt4 key购买 nike

我正在开发一个移动应用程序,它应该像谷歌地图导航应用程序一样工作。我从一个 kml 文件中获取路线信息,并在每个转折点为该位置创建一个接近警报。警报工作正常。每个警报都会触发一个通知。现在,我想在每个警报而不是通知之后设置我的 textView 中的文本信息。那么我怎样才能从我的广播接收器访问我的 map Activity 中的 textView。有人有想法吗?这是我的代码:

map Activity (基本部分...)

public class Map extends MapActivity implements OnClickListener {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.map);
....
....
this.addProximityAlert();

}

private void addProximityAlert() {
for (int i = 0; i < _navSet.getPlacemarks().size(); i++) {
int uniqueID = i + 1;
String text = _navSet.getPlacemarks().get(i).getTitle();
setProximityAlert(_navSet.getPlacemarks().get(i).getLatitude(),
_navSet.getPlacemarks().get(i).getLongitude(), text,
uniqueID, i);
}
}


private void setProximityAlert(double lat, double lon, String text,
long uniqueID, int requestCode) {
String intentAction = PROX_ALERT_INTENT + uniqueID; // each Intent must
// be unique
Intent intent = new Intent(intentAction);
// puts the text information(e.g.Turn left onto ... Road)
intent.putExtra(ProximityIntentReceiver.TEXT_INTENT_EXTRA, text);
PendingIntent proximityIntent = PendingIntent.getBroadcast(
getApplicationContext(), requestCode, intent,
PendingIntent.FLAG_CANCEL_CURRENT);

_locationManager.addProximityAlert(
lat, // the latitude of the central point of the alert region
lon, // the longitude of the central point of the alert region
POINT_RADIUS, // the radius of the central point of the alert region, in meters
PROX_ALERT_EXPIRATION, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration
proximityIntent // will be used to generate an Intent to fire when entry from the alert region is detected
);

IntentFilter filter = new IntentFilter(intentAction);
registerReceiver(new ProximityIntentReceiver(), filter);
}

还有我扩展 BroadcastReceiver 的类

public class ProximityIntentReceiver extends BroadcastReceiver {

private static final int NOTIFICATION_ID = 1000;
public static final String TEXT_INTENT_EXTRA = "text";
private TextView textView;

@Override
public void onReceive(Context context, Intent intent) {

String text = intent.getStringExtra(TEXT_INTENT_EXTRA);
String key = LocationManager.KEY_PROXIMITY_ENTERING;

Boolean entering = intent.getBooleanExtra(key, false);

if (entering) {
Log.d(getClass().getSimpleName(), "entering");

NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);
Notification notification = createNotification();
notification.setLatestEventInfo(context,
"Alert!", text, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);


/*doesn't work: View myView = (View) findViewById(R.layout.map, null);
textView = (TextView) myView.findViewById(R.id.descriptionView);
textView.setText(text); */
}
else {
Log.d(getClass().getSimpleName(), "exiting");
}
}
}

我使用 getStringExtra 获取信息并可以创建新通知。现在我想将此文本信息设置到我的 MapActivity 中的 TextView 中......但它不能以这种方式工作。感谢一切。

最佳答案

您可以使用类似以下内容的方式创建您的接收器:new ProximityIntentReceiver(theTextView) 一切顺利,

public ProximityIntentReceiver(TextView textView) {
this.textView = textView;
}

@Override
public void onReceive(Context context, Intent intent) {
// ...
if (entering) {
// ...
this.textView.setText(text);
} else {
// ...
}
}

关于Android:接近警报后的setText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10477095/

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