Android:如何设置多个警报

时间:2012-10-01 21:10:58

标签: android alarms

我必须同时设置3个不同的闹钟,但我目前正在尝试使用两个闹钟。我已经按照这样的教程Android: Multiple Alarm not working进行了操作? 但没有一个工作,我只能获得1报警。请帮助我弄清楚我做错了什么。这是我的代码有多个警报:

    //alarm1
                Intent alarmIntent = new Intent (Enter_med.this, MyAlarmService.class);

                //pendingIntentOfTwoMonth2 = PendingIntent.getBroadcast(this, 1, in, PendingIntent.FLAG_UPDATE_CURRENT);
                PendingIntent pendingAlarmIntent = PendingIntent.getService(Enter_med.this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

                Calendar AlarmCal = Calendar.getInstance();
                AlarmCal.setTimeInMillis(System.currentTimeMillis());
                AlarmCal.set(Calendar.HOUR_OF_DAY, pHour);
                AlarmCal.set(Calendar.MINUTE, pMinute);
                AlarmCal.set(Calendar.SECOND, 0);

                alarmManager.set(AlarmManager.RTC_WAKEUP, AlarmCal.getTimeInMillis(), pendingAlarmIntent);


                //alarm2
                PendingIntent pendingAlarmIntent2 = PendingIntent.getService(Enter_med.this, 1, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManager alarmManager2 = (AlarmManager)getSystemService(ALARM_SERVICE);

                Calendar AlarmCal2 = Calendar.getInstance();
                AlarmCal2.setTimeInMillis(System.currentTimeMillis());
                AlarmCal2.set(Calendar.HOUR_OF_DAY, pHour);
                AlarmCal2.set(Calendar.MINUTE, pMinute);
                AlarmCal2.set(Calendar.SECOND, 0);

                alarmManager2.set(AlarmManager.RTC_WAKEUP, AlarmCal2.getTimeInMillis(), pendingAlarmIntent2);

                Toast.makeText(Enter_med.this, "Saved", Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(context, MedSked.class);
                startActivity(intent);   



            }

        }
     });

这是我的闹钟接收器:

    public class MyAlarmService extends Service {

@Override
public void onCreate() {
    Toast.makeText(this, "MyAlarmService.onCreate", Toast.LENGTH_LONG).show();

}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

public void onDestroy() {
    super.onDestroy();
    Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
}

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    Bundle extras = intent.getExtras();
    String data = extras.getString("nel.example.alarms1");
    Toast.makeText(this, "MyAlarmService.onStart() data = " + data, 
            Toast.LENGTH_LONG).show();

    Intent alertIntent = new Intent();
    alertIntent.setClass(this, MyAlert.class);
    alertIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(alertIntent);

}

这是我的警告对话框:

    public class MyAlert extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_my_alert);

        AlertDialog alertDialog = new AlertDialog.Builder(MyAlert.this).create();
        alertDialog.setTitle("Med Reminder");

        alertDialog.setMessage("Time to take your medicine");
        alertDialog.setIcon(R.drawable.ic_launcher);

        alertDialog.setButton("OK", new DialogInterface.OnClickListener(){

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        });
        alertDialog.show();
    }



}
我明天会有截止日期。任何帮助将不胜感激。提前谢谢..

0 个答案:

没有答案