我正在尝试制作一个根据时间选择器设置警报的应用程序,我已经这样做了,但我希望将 mainactivity 中的编辑文本放入警报对话框中,该对话框在警报响起时显示。
我想将文本从 mainactivity 发送到 alertdemo。
MainActivity.class
private void stalarm() {
View.OnClickListener setClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
/** This intent invokes the activity DemoActivity, which in turn opens the AlertDialog window */
Intent i = new Intent("app.com.timepicker.demoactivity");
/** Creating a Pending Intent */
PendingIntent operation = PendingIntent.getActivity(getBaseContext(), unique, i, PendingIntent.FLAG_CANCEL_CURRENT);
/** Getting a reference to the System Service ALARM_SERVICE */
AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
/** Getting a reference to TimePicker object available in the MainActivity */
TimePicker tpTime = (TimePicker) findViewById(R.id.tp_time);
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = tpTime.getCurrentHour();
int minute = tpTime.getCurrentMinute();
/** Creating a calendar object corresponding to the date and time set by the user */
GregorianCalendar calendar = new GregorianCalendar(year, month, day, hour, minute);
/** Converting the date and time in to milliseconds elapsed since epoch */
long alarm_time = calendar.getTimeInMillis();
/** Setting an alarm, which invokes the operation at alart_time */
alarmManager.set(AlarmManager.RTC_WAKEUP, alarm_time, operation);
/** Alert is set successfully */
Toast.makeText(getBaseContext(), "Alarm is set successfully", Toast.LENGTH_SHORT).show();
}
};
Button btnSetAlarm = (Button) findViewById(R.id.btn_set_alarm);
btnSetAlarm.setOnClickListener(setClickListener);
}
demoactivity.class
public class DemoActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Creating an Alert Dialog Window */
AlertDemo alert = new AlertDemo();
/** Opening the Alert Dialog Window. This will be opened when the alarm goes off */
alert.show(getSupportFragmentManager(), "AlertDemo");
}
alertdemo.class
public class AlertDemo extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
/** Turn Screen On and Unlock the keypad when this alert dialog is displayed */
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
/** Creating a alert dialog builder */
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
/** Setting title for the alert dialog */
builder.setTitle("alarm " );
/** Setting the content for the alert dialog */
builder.setMessage("An Alarm by AlarmManager");
/** Defining an OK button event listener */
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
/** Exit application on click OK */
getActivity().finish();
}
});
/** Creating the alert dialog window */
return builder.create();
}
/** The application should be exit, if the user presses the back button */
@Override
public void onDestroy() {
super.onDestroy();
getActivity().finish();
}
}