待定意图立即触发

Pending Intent Fires Immediately

我正在尝试制作一个允许用户使用两个 TimePickers 输入两次的应用程序,并且 phone 将在这些时间之间设置为静音。我正在使用 PendingIntent 和 AlarmManager 在用户选择的时间触发 BroadcastReceiver。 目前,当用户单击保存按钮时,它会立即将 phone 设置为静音。然后我的 "alarm scheduled" toast 出现了,我的 "broadcast received" 出现了两次,但之后它什么也没做。

这是我的主要 activity 代码:

public class MainActivity extends AppCompatActivity {

AlarmManager alarm;
TimePicker timePickerStart;
TimePicker timePickerEnd;


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

    timePickerStart = (TimePicker)findViewById(R.id.timePickerStart);
    timePickerStart.setIs24HourView(true);
    timePickerEnd = (TimePicker)findViewById(R.id.timePickerEnd);
    timePickerEnd.setIs24HourView(true);

}


//method is called when save button is clicked
public void setAlarm(View view) {
    Calendar calendarStart;
    Calendar calendarEnd;

    calendarStart = Calendar.getInstance();
    calendarEnd = Calendar.getInstance();


    //Set calendars to the times in both TimePickers
    calendarStart.set(Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, timePickerStart.getCurrentHour(), timePickerStart.getCurrentMinute());
    long startTimeMillis = calendarStart.getTimeInMillis();

    calendarEnd.set(Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, timePickerEnd.getCurrentHour(), timePickerEnd.getCurrentMinute());
    long endTimeMillis = calendarEnd.getTimeInMillis();



    //create an intent and set the class that will be triggered by the intent
    Intent intent = new Intent(MainActivity.this, Receiver.class);
    PendingIntent pIntent = null;
    pIntent = PendingIntent.getBroadcast(this, 123, intent, PendingIntent.FLAG_UPDATE_CURRENT);


    //create alarm manager
    alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    //set alarms to go off at the time specified by the timePickers
    alarm.set(AlarmManager.RTC_WAKEUP, calendarStart.getTimeInMillis(), pIntent);
    alarm.set(AlarmManager.RTC_WAKEUP, calendarEnd.getTimeInMillis(), pIntent);

    //Toast for feedback
    Toast.makeText(this, "Alarm Scheduled", Toast.LENGTH_SHORT).show();

}

这是我的 BroadcastReceiver:

public class Receiver extends BroadcastReceiver {

AudioManager audioManager;
int modeNum;


@Override
public void onReceive(Context context, Intent intent) {

    //toast for feedback
    Toast.makeText(context, "Broadcast Received", Toast.LENGTH_SHORT).show();


    audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);

    modeNum = audioManager.getRingerMode();
    if (modeNum == 0) {
        audioManager.setRingerMode(2);
    } else {
        audioManager.setRingerMode(0);
    }
}

}

这是我的清单:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".Receiver"></receiver>
</application>

您有多个问题。

首先,您正尝试创建两个具有相同 PendingIntent 的闹钟。这是行不通的。您的第二个报警请求将取消第一个。您需要 distinct PendingIntent 个对象,这意味着要么 Intent 个对象被 PendingIntent 个对象包裹,要么使用两个不同的值 [= =14=].

其次,您对 Calendarset() 调用不正确。例如,在这两个地方,您的第一个参数都应该是年份。您传递的是 Calendar.YEAR,即 1,而不是 2015