2012-05-07 10 views
1

私はGoogleマップのナビゲーションアプリのように動作するモバイルアプリケーションを開発しています。私はkmlファイルからルート情報を取得し、各ターンポイントでこの位置の近接アラートを作成しました。アラートは正常に機能します。各アラートによって通知がトリガーされます。さて、私は通知の代わりに各警告の後に、私のtextViewのtextinformationを設定したいと思います。では、私のマップアクティビティでブロードキャストレシーバからテキストビューにアクセスするにはどうしたらいいですか?誰かアイデアはありますか?私はgetStringExtraとの情報を入手し、新しい通知を作成することができますBroadcastReceiverAndroid:近接アラート後のsetText

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

を拡張

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

地図アクティビティ(要部...)そして、私のクラス:これは私のコードです。今私はこのテキスト情報をMapActivityのテキストビューに設定したいと思いますが、このようには動作しません。すべてに感謝します。

答えて

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 { 
    // ... 
    } 
} 
+0

こんにちは!ありがとうございました。あなたが正しい!今それは動作します。ありがとう! – Martin

-1

は、このコードを試してみてください。これはあなたを助けるかもしれない

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

こんにちは!ご協力いただきありがとうございます。しかし、メソッドsetContentViewは放送受信機のために定義されていません。とにかくありがとう! – Martin

関連する問題