Android 中的 AlarmReceiver 从不接收警报

发布于 2024-12-19 00:55:59 字数 1821 浏览 3 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文