Search code examples
androidbroadcastreceiverproximitysettext

Android: setText after proximity alert


I'm developing a mobile application which should works like the google map navigation app. I get the route information from a kml file and at each turn point I created a proximity alert for this position. The alerts work fine. Each alert triggers a notification. Now, I would like set after each alert instead of the notification, a textinformation in my textView. So how can I get access from my the Broadcast Receiver to my textView in my Map Activity. Has anybody an idea? This is my code:

Map Activity (essential parts...)

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

And my Class which extends 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");
    }
}
}

I get the information with getStringExtra and can create a new notification. Now I would like to set this text information into a text view in my MapActivity..... but It doesn't work in this way. Thanks for everything.


Solution

  • You can create your receiver with something like: new ProximityIntentReceiver(theTextView) and you are good to go,

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