Google マップ ナビゲーション アプリのように動作するモバイル アプリケーションを開発しています。kml ファイルからルート情報を取得し、各ターン ポイントでこの位置の近接アラートを作成しました。アラートは正常に機能します。各アラートは通知をトリガーします。ここで、通知の代わりに各アラートの後に、textView のテキスト情報を設定したいと思います。では、Broadcast Receiver から Map アクティビティの 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 のテキスト ビューに設定したいと思います.....しかし、この方法では機能しません。すべてに感謝します。