我正在开发一个应该像谷歌地图导航应用程序一样工作的移动应用程序。我从 kml 文件中获取路线信息,并在每个转折点为该位置创建接近警报。警报工作正常。每个警报都会触发一个通知。现在,我想在每个警报而不是通知之后设置我的 textView 中的文本信息。那么如何在我的地图活动中从我的广播接收器访问我的 textView。有人有想法吗?这是我的代码:
地图活动(基本部分...)
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 中的文本视图中......但它不能以这种方式工作。感谢一切。