将接收器和服务添加到Android提醒应用

时间:2015-08-05 02:21:47

标签: android service broadcastreceiver alarmmanager android-alarms

我正在使用AlarmManager创建一个Android提醒应用程序。我的主要活动的GUI包含一个时间和日期选择器对话框以及一个调用setReminder函数的按钮,该函数获取用户输入的时间和日期,并为该特定日期和时间设置提醒。当提醒时间发生时,它会移动到另一个活动,其GUI包含用于延后和取消的按钮。我用MediaPlayer播放了闹钟。两个按钮都运行良好。我希望我的闹钟在后台运行,即使应用程序已关闭并希望我的应用程序通知操作系统有关警报的信息。这就是我想为这个应用程序添加广播接收器和服务的原因。我怎么能这样做?

主要活动代码:

 public class MainActivity extends Activity {
    AlarmManager alarmMgr;


   private Calendar calendar;
   private TextView dateView;
   private int year, month, day;
   private int hour,min;
  // private int sYear, sMonth, sDay;
   //private int sHour,sMin;


   private EditText time;
   private String format = "";
   PendingIntent pi;




 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dateView = (TextView) findViewById(R.id.editText2);
    calendar = Calendar.getInstance();
    year = calendar.get(Calendar.YEAR);

    month = calendar.get(Calendar.MONTH);
    day = calendar.get(Calendar.DAY_OF_MONTH);
    showDate(year, month+1, day);


      time = (EditText) findViewById(R.id.editText3);
      calendar = Calendar.getInstance();

      hour = calendar.get(Calendar.HOUR_OF_DAY);
      min = calendar.get(Calendar.MINUTE);
      showTime(hour, min);       


}
  @SuppressWarnings("deprecation")
  public void setDate(View view) {
   showDialog(999);
   Toast.makeText(getApplicationContext(), "ca", Toast.LENGTH_SHORT)
   .show();
   }
  @Override
  protected Dialog onCreateDialog(int id) {
   // TODO Auto-generated method stub
   if (id == 999) {
      return new DatePickerDialog(this,myDateListener, year, month, day);
   }
   else if (id == 1111) {
       return new TimePickerDialog(this,myTimeListener, hour,min,true);
    }
   return null;
  }

 private DatePickerDialog.OnDateSetListener myDateListener = new  DatePickerDialog.OnDateSetListener() {
   @Override
   public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
      // TODO Auto-generated method stub

      showDate(arg1, arg2+1, arg3);
   }
   };

  private void showDate(int year, int month, int day) {
   dateView.setText(new StringBuilder().append(day).append("/")
   .append(month).append("/").append(year));
   this.year=year;
   this.month=(month-1);
   this.day=day; 

 }


 @SuppressWarnings("deprecation")
 public void setTime(View view) {
 showDialog(1111);
 Toast.makeText(getApplicationContext(), "set", Toast.LENGTH_SHORT)
 .show();
 }


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

@Override
public void onTimeSet(TimePicker arg0, int arg1, int arg2) {
    showTime(arg1,arg2);

}

};
public void showTime(int hour, int min) {

   time.setText(new StringBuilder().append(hour).append(" : ").append(min)
      .append(" ").append(format));

      this.hour= hour;
      this.min= min;
   }  


    public void setAlarm(View v) 
   { 



   alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
   Intent intent = new Intent(getApplicationContext(),Alarm.class);

    PendingIntent                pi=PendingIntent.getActivity(getApplicationContext(),0,intent,0);


    calendar.setTimeInMillis(System.currentTimeMillis());
    Calendar calendar = Calendar.getInstance();
     calendar.set(Calendar.MONTH, month);
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.DAY_OF_MONTH, day); 

    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, min);
  long timeToAlarm = calendar.getTimeInMillis();



  alarmMgr.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), pi); 



}


     @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

主要xml

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity" >

  <EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignLeft="@+id/button1"
    android:ems="10" >

    <requestFocus />
 </EditText>

 <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:onClick="setAlarm"
    android:text="Set" />

 <EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/button2"
    android:layout_alignParentRight="true"
    android:ems="10" />

 <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:layout_alignLeft="@+id/textView2"
    android:layout_marginBottom="33dp"
    android:onClick="setDate"
    android:text="Set Date" />

 <EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="104dp"
    android:ems="10" />

 <Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/editText3"
    android:layout_alignBottom="@+id/editText3"
    android:layout_alignLeft="@+id/textView2"
    android:onClick="setTime"
    android:text="SetTime" />

 <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="67dp"
    android:text="Set Hobby"
    android:textAppearance="?android:attr/textAppearanceLarge" />

  </RelativeLayout>

Alarm.java

 public class Alarm extends Activity {
   MediaPlayer mp;
   AlarmManager AlmMgr;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alarm);
    mp=MediaPlayer.create(getApplicationContext(), R.raw.tum);
    mp.start();
  }
public void onCancel(View v)
{
 mp.stop();
 mp=null;
 }

 public void onSnooze(View v)
 {
  mp.stop();
  AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);

  Calendar calendar = Calendar.getInstance();

  calendar.add(Calendar.MINUTE, 1); 
  long snoozeTime = calendar.getTimeInMillis();

  Intent AlarmIntent = new Intent(getApplicationContext(),this.getClass());
  AlarmIntent.putExtra("REQUEST CODE", 1);


  PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 1, AlarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);   
  AlmMgr.set(AlarmManager.RTC_WAKEUP, snoozeTime, pi);

 }




 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_alarm, menu);
    return true;
 }

}

activity_alarm_xml

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".Alarm" >

  <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="67dp"
    android:onClick="onCancel"
    android:text="Cancel" />

  <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="48dp"
    android:text="Snooze"
    android:onClick="onSnooze" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

  

我想在此应用程序中添加广播接收器和服务。我怎么能这样做?

嗯,除非你告诉我们两者的目的是什么,你想用它做什么,以及你已经尝试过什么,否则将无法提供帮助。

添加 BradcastReceiver 类:

public class MySnoozeReciever extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent intent = new Intent(context, Alarm .class);
       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       startActivity(intent);
    }
}

别忘了清单:

 <receiver android:name='.MySnoozeReciever'>
    </receiver>

修改这些行:

Intent intent = new Intent(getApplicationContext(), MySnoozeReciever.class);

PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(),0,intent,0);