我正在开发一个移动应用程序,该应用程序的工作方式应类似于Google地图导航应用程序。我从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 .....中的文本视图,但是这种方式不起作用。谢谢你所做的一切。
最佳答案
您可以使用以下内容创建接收器: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 {
// ...
}
}