将信息从CustomDialog传递到主Activity

时间:2012-08-27 00:33:07

标签: android android-intent broadcastreceiver customdialog

我正在编写一个应用程序来管理日历应用程序中的警报和事件。当我试图获取有关自定义对话框中报告的事件的所有信息时,我遇到了问题。我尝试了很多东西。我在互联网上看到的最后一件事是使用意图。但是,只要对话框可以发送信息,我就不知道如何接收它。我尝试使用广播接收器,但它没有用......

- >以下是对话框的代码:

public class AddingEventMenu extends Dialog implements OnClickListener {

Event event = new Event();
private Button b;
Context context;

public AddingEventMenu(Context context) {
    super(context);
    this.context = context;
    this.setContentView(R.layout.addbuttonlayout);
    this.setTitle("Adding Event");
    this.setCancelable(true);

    this.b = (Button) findViewById(R.id.addEventButton);
    this.b.setOnClickListener(this);

}

public void onClick(View v) {
    String color;
    DatePicker DP = (DatePicker) findViewById(R.id.datePicker);
    TimePicker TP = (TimePicker) findViewById(R.id.timePicker);
    EditText ET = (EditText) findViewById(R.id.eventText);
    Spinner SC = (Spinner) findViewById(R.id.colorSelector);
    Intent intent = new Intent();
    intent.setAction("New Event");


    color = SC.getSelectedItem().toString();

    if (color == "Red") event.eventColor = Color.RED;
    if (color == "Green") event.eventColor = Color.GREEN;
    if (color == "Blue") event.eventColor = Color.BLUE;
    if (color == "Magenta") event.eventColor = Color.MAGENTA;

    intent.putExtra("Text", ET.getText().toString());
    intent.putExtra("Day", DP.getDayOfMonth());
    intent.putExtra("Month", DP.getMonth());
    intent.putExtra("Year",  DP.getYear());
    intent.putExtra("Hour",  TP.getCurrentHour() * 100 + TP.getCurrentMinute());
    intent.putExtra("Color", color);

    context.sendBroadcast(intent);

    Log.d("State", "Es un primer paso");

    this.dismiss();


}

- >这里是主菜单代码:

public class Timetable extends Activity {
public static List<Event> eventList;
EventManager eventManager;
LinearLayout eventListLayout;
ImageView addButton;
EventDatabaseManager db;
BroadcastReceiver BR;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.layout_timetable);

    BR = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction() == "New event"){
                Bundle extras = new Bundle();
                Event event = new Event();
                extras = intent.getExtras();
                event.eventText = extras.getString("Text");
                event.eventDay = extras.getInt("Day");
                event.eventMonth = extras.getInt("Month");
                event.eventYear = extras.getInt("Year");
                event.eventHour = extras.getInt("Hour");
                event.eventColor = extras.getInt("Color");

                Log.d("State", "Esta funcionando");

                saveNewEvent(event);
            }
        }
    };

    db = new EventDatabaseManager(this);

    eventListLayout = (LinearLayout) findViewById(R.id.eventListLayout);

    eventManager = new EventManager(this);
    eventList = eventManager.getEvents();



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.layout_timetable, menu);
    return true;
}

public void onAddButton(View button) {
    AddingEventMenu aEM = new AddingEventMenu(this);

    aEM.show();

}

public void saveNewEvent(Event event) {
    eventList.add(event);
    db.addEvent(event);

}

在主要的活动中,我定义了广播接收者,但我认为这是错误的,因为它没有任何东西。

我希望有人可以帮助我。谢谢你的时间。

2 个答案:

答案 0 :(得分:1)

您注册了广播接收器。你可以这样做:

IntentFilter filter =  new IntentFilter();
filter.addAction( "New Event" );
registerReceiver( BR, observerFilter );

您应该将此代码放在onCreate方法中。不要忘记使用

取消注册接收器
unregisterReceiver( BR );
在onDestroy方法中

你还使用

 intent.setAction("New Event");
在你的onClick方法中

,但在你的onReceive方法中使用

if( intent.getAction() == "New event" )

将其更改为 if( intent.getAction() == "New Event" )

答案 1 :(得分:0)

为了将来兼容,您应该考虑使用DialogFragment代替 Dialog

使用DialogFragment时,您可以向DialogFragment类添加自定义侦听器,如下所示:

public interface EventAddedListener {
  public void onEventAdded(Event event);
}

然后将其添加到您的DialogFragment

选择活动后,您拨打listener.onEventAdded(event);并在Activity内处理结果。

但即使使用IntentFilter,您也可以使用sinisha提供的更复杂的DialogFragment解决方案。