【问题标题】:Android: setText after proximity alertAndroid:接近警报后的setText
【发布时间】:2012-05-07 05:29:46
【问题描述】:

我正在开发一个移动应用程序,它应该像谷歌地图导航应用程序一样工作。我从 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);
}

还有我的扩展广播接收器的类

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 中的文本视图中......但它不能以这种方式工作。谢谢你的一切。

【问题讨论】:

    标签: android broadcastreceiver proximity settext


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      试试这个代码。这可能对你有帮助

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

      【讨论】:

      • 嗨!谢谢你的帮助。但是 setContentView 方法对于广播接收器是未定义的。还是谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      • 1970-01-01
      相关资源
      最近更新 更多