Android 中的 AlarmReceiver 从不接收警报
我的 AlarmManager 类扩展了 BroadcastReceiver ,我已经尝试了所有方法,但它似乎从未收到我的警报。
这是我在主类中的方法
public void setApp(int startHour, int startMin, int endHour, int endMin) {
Intent intent = new Intent(this, AlarmManager.class);
PendingIntent sender = PendingIntent.getBroadcast(PhoneManagerActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Toast.makeText(PhoneManagerActivity.this, "Start Alarm", Toast.LENGTH_LONG).show();
Calendar calendar = Calendar.getInstance();
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTimeInMillis(System.currentTimeMillis());
startCalendar.add(Calendar.SECOND, 10);
if (startCalendar.getTimeInMillis() <= calendar.getTimeInMillis()) {
startCalendar.add(Calendar.HOUR, 24);
}
Am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),AlarmManager.INTERVAL_DAY, sender);
}
这是我的接收器类
public class AlarmManager extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("beast", "alarm");
try {
Log.i("beast", "alarm recieved");
Toast.makeText(context, "Phone set",Toast.LENGTH_LONG).show();
// AdamApp3Activity adam = new AdamApp3Activity();
((PhoneManagerActivity)context).setRinger();
Intent newIntent = new Intent(context, PhoneManagerActivity.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
我已将其定义为 xml 中的接收器,但它从未收到我的警报
My AlarmManager class extends BroadcastReceiver and Ive tried everything but it never seems to receive my alarm.
here is my method in the main class
public void setApp(int startHour, int startMin, int endHour, int endMin) {
Intent intent = new Intent(this, AlarmManager.class);
PendingIntent sender = PendingIntent.getBroadcast(PhoneManagerActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Toast.makeText(PhoneManagerActivity.this, "Start Alarm", Toast.LENGTH_LONG).show();
Calendar calendar = Calendar.getInstance();
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTimeInMillis(System.currentTimeMillis());
startCalendar.add(Calendar.SECOND, 10);
if (startCalendar.getTimeInMillis() <= calendar.getTimeInMillis()) {
startCalendar.add(Calendar.HOUR, 24);
}
Am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),AlarmManager.INTERVAL_DAY, sender);
}
Here is my receiver class
public class AlarmManager extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("beast", "alarm");
try {
Log.i("beast", "alarm recieved");
Toast.makeText(context, "Phone set",Toast.LENGTH_LONG).show();
// AdamApp3Activity adam = new AdamApp3Activity();
((PhoneManagerActivity)context).setRinger();
Intent newIntent = new Intent(context, PhoneManagerActivity.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
I have defined it as a receiver in my xml but it never receives my alarm
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论