等待意图立即触发

时间:2015-07-05 15:38:53

标签: android alarmmanager android-pendingintent

我正在尝试创建一个允许用户使用两个TimePickers输入两次的应用程序,并且手机将在这些时间之间设置为静音。我正在使用PendingIntent和AlarmManager在用户选择的时间触发BroadcastReceiver。 此时,当用户单击保存按钮时,它会立即打开和关闭手机静音。然后我的“闹钟预定”吐司出现,我的“收到的广播”出现两次,但之后没有做任何事情。

这是我的主要活动代码:

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>

1 个答案:

答案 0 :(得分:1)

你有很多问题。

首先,您尝试使用相同的PendingIntent创建两个警报。那样不行。您的第二个警报请求将取消第一个警报请求。您需要不同的 PendingIntent个对象,这意味着Intent个对象包含的PendingIntent对象要么大不相同,要么使用两个不同的值{{1} }。

其次,123set()的{​​{1}}来电不正确。例如,在这两个地方,您的第一个参数应该是年份。您正在传递Calendar Calendar.YEAR,而不是1