我正在编写一个像日历应用程序一样管理警报和事件的应用程序。当我尝试获取有关自定义对话框中报告的事件的所有信息时遇到了问题。我尝试了很多东西。我在互联网上看到的最后一件事是使用意图。但是对话框可以发送信息,我不知道如何接收它。我用广播接收器尝试了一些东西,但它没有用......
-->这是对话框的代码:
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);
}
在主活动中,我定义了广播接收器,但我认为这是错误的,因为它在那里什么都没有。
我希望有一个人可以帮助我。谢谢你的时间。