如何使用android报警管理器设置多个重复挂起的意图?

时间:2014-04-17 13:48:54

标签: android android-intent alarmmanager android-pendingintent

我正在制作一款应用程序,我需要在预定时间将手机转为振动模式。 问题是只执行第二个待处理的意图。 如何启动多个待处理意图?

我的代码:

import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

public class GetSlots extends Activity {

    private Calendar mCalendar;  
    private Calendar nCalendar;
    private CheckBox a1;

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

        a1 = (CheckBox)findViewById(R.id.checkBox2);
    }

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

    public void addenable() {
    }

    public void getdata(View V) {

        mCalendar= Calendar.getInstance();
        mCalendar.set(Calendar.HOUR_OF_DAY, 16 );
        mCalendar.set(Calendar.MINUTE, 27);
        mCalendar.set(Calendar.SECOND, 0);

        nCalendar= Calendar.getInstance();
        nCalendar.set(Calendar.HOUR_OF_DAY, 16 );
        nCalendar.set(Calendar.MINUTE, 29);
        nCalendar.set(Calendar.SECOND, 0);

        Intent intentAlarm = new Intent();
        intentAlarm.setClass(this, AlarmReciever.class);

        PendingIntent i = PendingIntent.getBroadcast(this, 0, intentAlarm,
                PendingIntent.FLAG_UPDATE_CURRENT);

        PendingIntent j = PendingIntent.getBroadcast(this, 0, intentAlarm,
                PendingIntent.FLAG_UPDATE_CURRENT);

        // create the object
        AlarmManager mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        if(a1.isChecked()) {
            mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(),500*1000, i);
            Toast.makeText(this, "Alarm Scheduled", Toast.LENGTH_LONG).show();

            mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, nCalendar.getTimeInMillis(), 1000*1000, j);
            Toast.makeText(this, "Alarm Scheduled", Toast.LENGTH_LONG).show();
        }

        if(!a1.isChecked()) {
            PendingIntent.getBroadcast(this, 0, intentAlarm,
                    PendingIntent.FLAG_UPDATE_CURRENT).cancel();
        }
    }
}

0 个答案:

没有答案