gpt4 book ai didi

java - AlarmManager 在 Android 中不工作

转载 作者:行者123 更新时间:2023-12-01 10:13:19 25 4
gpt4 key购买 nike

我正在学习 AlarmManager 的教程。我已经在模拟器中运行了教程的代码,但警报没有响起。另外,教程在Calendar的第一个java类中使用了clone,为什么这里要clone呢?

任何帮助将不胜感激。

这是代码:

AndroidTimeActivity.java

public class AndroidTimeActivity extends Activity {

TimePicker myTimePicker;
Button buttonstartSetDialog;
TextView textAlarmPrompt;

TimePickerDialog timePickerDialog;

final static int RQS_1 = 1;

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

textAlarmPrompt = (TextView)findViewById(R.id.alarmprompt);

buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog);
buttonstartSetDialog.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
textAlarmPrompt.setText("");
openTimePickerDialog(false);

}
});

}


private void openTimePickerDialog(boolean is24r){
Calendar calendar = Calendar.getInstance();

timePickerDialog = new TimePickerDialog(
AndroidTimeActivity.this,
onTimeSetListener,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
is24r);
timePickerDialog.setTitle("Set Alarm Time");

timePickerDialog.show();

}

OnTimeSetListener onTimeSetListener
= new OnTimeSetListener(){

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();

calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
calSet.set(Calendar.MINUTE, minute);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);

if(calSet.compareTo(calNow) <= 0){
//Today Set time passed, count to tomorrow
calSet.add(Calendar.DATE, 1);
}

setAlarm(calSet);
}
};

private void setAlarm(Calendar targetCal){

textAlarmPrompt.setText(
"\n\n***\n"
+ "Alarm is set@ " + targetCal.getTime() + "\n"
+ "***\n");

Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

}

}

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context arg0, Intent arg1) {
Toast.makeText(arg0, "Alarm received!", Toast.LENGTH_LONG).show();
}
}

AndroidManifest.xml

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />


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

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

<Receiver
android:name=".AlarmReciever"
android:label="@string/title_activity_alarm_reciever"
android:theme="@style/AppTheme.NoActionBar">
</Receiver>
</application>

最佳答案

“Receiver”在 list 中拼写错误两次:

android:name=".AlarmReciever"
android:label="@string/title_activity_alarm_reciever"

应该是:

android:name=".AlarmReceiver"
android:label="@string/title_activity_alarm_receiver"

关于java - AlarmManager 在 Android 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36040434/

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