Android:接近警报后的setText

时间:2012-05-07 05:29:46

标签: android broadcastreceiver proximity settext

我正在开发一个应该像谷歌地图导航应用程序一样工作的移动应用程序。我从kml文件中获取路线信息,并在每个转弯点为此位置创建了接近警报。警报工作正常。每个警报都会触发通知。现在,我想在每个警报后设置而不是通知,我的textView中的文本信息。那么如何从我的广播接收器访问我的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);
}

我的Class扩展了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中将此文本信息设置为文本视图.....但它不能以这种方式工作。谢谢你的一切。

2 个答案:

答案 0 :(得分:2)

您可以使用以下内容创建接收器: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 {
    // ...
    }
}

答案 1 :(得分:-1)

试试这段代码。这可能会对你有所帮助

 setContentView(R.layout.map);
 textView = (TextView)findViewById(R.id.descriptionView);
 textView.setText(text);