如何设置多个警报并相应删除它们

时间:2013-01-25 08:23:06

标签: android alarm

我想开发一个超过1个闹钟的闹钟。 假设我必须同时发出5个警报,现在我无法为每个警报提供特定的id,以便当用户点击相应的取消按钮时我可以取消该特定警报 我的代码是这样的

    on1=(ToggleButton) findViewById(R.id.toggleButton1);
    on2=(ToggleButton) findViewById(R.id.toggleButton2);
    on3=(ToggleButton) findViewById(R.id.toggleButton3);
    on4=(ToggleButton) findViewById(R.id.toggleButton4);
    on5=(ToggleButton) findViewById(R.id.toggleButton5);

    on1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            en=on1.isChecked();
            getdeta(1, en);     
            setalarm(en, m, h,1);
        }
    });

    on2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            en=on2.isChecked();
            getdeta(2, en);     
            setalarm(en, m, h,2);
        }
    });

    on3.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            en=on3.isChecked();
            getdeta(3, en); 
            setalarm(en, m, h,3);
        }
    });

    on4.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            en=on4.isChecked();
            getdeta(4, en);     
            setalarm(en, m, h,4);
        }
    });

    on5.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            en=on5.isChecked();
            getdeta(5, en); 
            setalarm(en, m, h,5);
        }
    });

和meathods是: -

public void getdeta(int number,boolean enable){
    String MYPR=(""+number);
    if(enable=true){
        SharedPreferences alarm=getSharedPreferences(MYPR, 0);
        SharedPreferences.Editor edit=alarm.edit();
        edit.putBoolean("en", true);
        edit.commit();
    }
    else{
        SharedPreferences alarm=getSharedPreferences(MYPR, 0);
        SharedPreferences.Editor edit=alarm.edit();
        edit.putBoolean("en", false);
        edit.commit();
    }

    SharedPreferences set1=getSharedPreferences(MYPR, 0);
    h=set1.getInt("Ahh", 00);
    m=set1.getInt("Amm", 00);
    en=set1.getBoolean("en", false);
}

public void setalarm (boolean en,int mm,int hh,int number)
{
    // meathod to set the alarm time and its enability
    if(en==true)
    {   //create an alarm calendar
        Calendar Alarm=Calendar.getInstance();
        Alarm.set(Calendar.MILLISECOND, 0);
        Alarm.set(Calendar.SECOND, 0);
        Alarm.set(Calendar.MINUTE, mm);
        Alarm.set(Calendar.HOUR_OF_DAY,hh);
        Alarm.set(Calendar.DAY_OF_WEEK, 2);

        //creating and assigning value to alarm manager class
        Intent AlarmIntent = new Intent(Multiple.this, AlarmReciever.class);
        AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
        PendingIntent Sender = PendingIntent.getBroadcast(Multiple.this, number, AlarmIntent, 0);    
        AlmMgr.set(AlarmManager.RTC_WAKEUP, Alarm.getTimeInMillis(), Sender);
    }
    else
    {   //Build Intent/Pending Intent for canceling the alarm
        Intent AlarmIntent = new Intent(Multiple.this, AlarmReciever.class);
        AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
        PendingIntent Sender = PendingIntent.getBroadcast(Multiple.this, number, AlarmIntent, 0);  
        AlmMgr.cancel(Sender);
    }
}

任何人都可以帮我创建闹钟............

0 个答案:

没有答案