gpt4 book ai didi

android - 待定 Intent 立即触发

转载 作者:行者123 更新时间:2023-11-30 02:02:45 24 4
gpt4 key购买 nike

我正在尝试制作一个应用程序,允许用户使用两个 TimePickers 输入两次,并且手机将在这些时间之间设置为静音。我正在使用 PendingIntent 和 AlarmManager 在用户选择的时间触发 BroadcastReceiver。目前,当用户单击保存按钮时,它会立即将手机设置为静音。然后出现我的“已安排警报” toast ,我的“已接收广播”出现两次,但此后它什么也没做。

这是我的主要 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();

}

这是我的广播接收器:

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);
}
}

这是我的 list :

<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 对象,这意味着由 PendingIntent 对象包裹的完全不同的 Intent 对象,或者使用两个123 的不同值。

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

关于android - 待定 Intent 立即触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31232022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com