Android /报警管理器/具体时间和日期

时间:2013-12-18 17:29:18

标签: android alarmmanager

你好,stackoverflow的人,

我想编写一个带有部分的应用程序,以便在特定时间和日期发出警报。理想情况下,它会触发通知,但如果警报能够正常工作,我几乎会感到高兴。我尝试了一些教程和示例,但没有一个彻底解释(如果你知道一个网站请发送链接)。我想现在我有各种各样的尝试。 这是应该收集关于警报何时起飞并触发它的信息的活动:

public class Activity_new_termin extends Activity {

 Button btnSelectDate,btnSelectTime, submit;
 EditText mTitle, mDescr;
 DatePicker pickerDate;
 TimePicker pickerTime;

 static final int DATE_DIALOG_ID = 0;
 static final int TIME_DIALOG_ID = 1;
 final static int RQS_1 = 1;

 public  int year,month,day,hour,minute;  
 private int mYear, mMonth, mDay,mHour,mMinute;

 private string date_name;
 private string descr;

 TextView info;

 public Activity_new_termin()
 {
             final Calendar c = Calendar.getInstance();
             mYear = c.get(Calendar.YEAR);
             mMonth = c.get(Calendar.MONTH);
             mDay = c.get(Calendar.DAY_OF_MONTH);
             mHour = c.get(Calendar.HOUR_OF_DAY);
             mMinute = c.get(Calendar.MINUTE);
 }

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_new_termin);

             btnSelectDate=(Button)findViewById(R.id.buttonSelectDate);
             btnSelectTime=(Button)findViewById(R.id.buttonSelectTime);
             submit=(Button)findViewById(R.id.butt_submit_newdate);
             mTitle=(EditText)findViewById(R.id.editText_datename_newdate);
             mDescr=(EditText)findViewById(R.id.editText_datedescr_newdate);

             btnSelectDate.setOnClickListener(new View.OnClickListener() {

                 public void onClick(View v) {
                      showDialog(DATE_DIALOG_ID);
                 }
             });

             btnSelectTime.setOnClickListener(new View.OnClickListener() {

                 public void onClick(View v) {
                      showDialog(TIME_DIALOG_ID);
                 }
             });

             submit.setOnClickListener(
                        new View.OnClickListener()
                        {
                            public void onClick(View view)
                            {

                                Log.v("editText_datename_newdate", mTitle.getText().toString());
                                Log.v("editText_datedescr_newdate", mDescr.getText().toString());

                                Calendar current = Calendar.getInstance();

                                Calendar cal = Calendar.getInstance();

                                cal.set(Calendar.YEAR, year);
                                cal.set(Calendar.MONTH, month);
                                cal.set(Calendar.DAY_OF_MONTH, day);
                                cal.set(Calendar.HOUR_OF_DAY, hour);
                                cal.set(Calendar.MINUTE, minute);
                                cal.set(Calendar.SECOND, 0);
                                cal.set(Calendar.MILLISECOND, 0);


                                if(cal.compareTo(current) <= 0){

                                    Toast.makeText(getApplicationContext(), 
                                      "Invalid Date/Time", 
                                      Toast.LENGTH_LONG).show();
                                }else{
                                 setAlarm(cal);
                                }

                            }
                        });
 }



  private DatePickerDialog.OnDateSetListener mDateSetListener =
                         new DatePickerDialog.OnDateSetListener() {

                             public void onDateSet(DatePicker view, int yearSelected,
                                                   int monthOfYear, int dayOfMonth) {
                                year = yearSelected;
                                month = monthOfYear;
                                day = dayOfMonth;

                                btnSelectDate.setText("Date selected : "+day+"-"+month+"-"+year);
                             }
                         };


                         private TimePickerDialog.OnTimeSetListener mTimeSetListener =
                             new TimePickerDialog.OnTimeSetListener() {

                                 public void onTimeSet(TimePicker view, int hourOfDay, int min) {
                                     hour = hourOfDay;
                                     minute = min;

                                     btnSelectTime.setText("Time selected :"+hour+"-"+minute);
                                   }
                             };

                         @Override
                         protected Dialog onCreateDialog(int id) {
                             switch (id) {
                             case DATE_DIALOG_ID:

                                 return new DatePickerDialog(this,
                                             mDateSetListener,
                                             mYear, mMonth, mDay);

                             case TIME_DIALOG_ID:
                                 return new TimePickerDialog(this,
                                         mTimeSetListener, mHour, mMinute, false);  

                             }
                             return null;
                         }

                         private void setAlarm(Calendar targetCal){

                              info.setText("\n\n***\n"
                                + "Alarm is set@ " + targetCal.getTime() + "\n"
                                + "***\n");

                              Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
                              PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
                              AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                              alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);   
                             }                         
}

及其xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="470dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<TextView
    android:id="@+id/textview_title_newdate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:text="@string/title_new_termin" />

    <EditText
    android:id="@+id/editText_datename_newdate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textview_title_newdate"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="36dp"
    android:ems="10"
    android:inputType="textMultiLine" />

 <EditText
     android:id="@+id/editText_datedescr_newdate"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignLeft="@+id/buttonSelectDate"
     android:layout_below="@+id/buttonSelectTime"
     android:layout_marginTop="27dp"
     android:ems="10" >

     <requestFocus />
 </EditText>

 <Button
     android:id="@+id/butt_submit_newdate"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignLeft="@+id/textview_title_newdate"
     android:layout_below="@+id/editText_datedescr_newdate"
     android:layout_marginTop="38dp"
     android:text="@string/butt_newdate_submit" />

 <Button
     android:id="@+id/buttonSelectDate"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@+id/editText_datename_newdate"
     android:layout_centerHorizontal="true"
     android:layout_marginTop="20dp"
     android:text="Select Date" />

 <Button
     android:id="@+id/buttonSelectTime"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignLeft="@+id/editText_datedescr_newdate"
     android:layout_below="@+id/buttonSelectDate"
     android:layout_marginTop="14dp"
     android:text="Select Time" />

</RelativeLayout>
</ScrollView>

最后是闹剧接收者:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver
{
         @Override
            public void onReceive(Context context, Intent intent)
            {

                    // Show the toast  like in above screen shot
                    Toast.makeText(context, "Alarm ausgelöst", Toast.LENGTH_LONG).show();


             }

}

提前非常感谢你!

1 个答案:

答案 0 :(得分:1)

我测试了你的代码。您在NullPointerException

上收到TextView info;

您已声明TextView info,但您从未指定过它,例如

info = (TextView) findViewById(R.id.what_ever);

之后,您可以像使用

一样使用它
info.setText("\n\n***\n"+ "Alarm is set@ " + targetCal.getTime() + "\n" + "***\n");