1

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

-->这是对话框的代码:

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

}

在主活动中,我定义了广播接收器,但我认为这是错误的,因为它在那里什么都没有。

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

4

2 回答 2

1

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

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

于 2012-08-27T08:03:24.833 回答
0

为了将来兼容,您应该考虑使用DialogFragment而不是 Dialog

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

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

然后将其添加到您的DialogFragment.

选择一个事件后,您调用listener.onEventAdded(event);并处理您的Activity.

但您也可以使用sinishaIntentFilter提供的更复杂的解决方案,即使在使用.DialogFragment

于 2012-08-27T08:51:42.530 回答