广播接收器不工作:Android

发布于 2024-12-12 12:01:45 字数 1780 浏览 2 评论 0原文

我正在为 BroadcastReceiver 实现一个示例代码,它将从特定时间开始,但是我的代码中有一些问题并且无法正常工作。

这是我的代码:

在 Activity 中:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Calendar cur_cal = new GregorianCalendar(TimeZone.getTimeZone("IST"), Locale.US);
        cur_cal.setTimeInMillis(System.currentTimeMillis());

        Calendar cal = new GregorianCalendar();
        cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
        cal.add(Calendar.HOUR_OF_DAY, 13);
        cal.add(Calendar.MINUTE, 10);
        cal.add(Calendar.SECOND, 00);
        cal.add(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DATE, 31);
        cal.add(Calendar.MONTH, 10);

        Intent intent = new Intent(HelloAlarmManagerActivity.this, ScheduleHandler.class);
        PendingIntent pIntent = PendingIntent.getBroadcast(HelloAlarmManagerActivity.this, 0, intent, 0);

        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 2*10, pIntent);
        Toast.makeText(HelloAlarmManagerActivity.this, "Alarm seted", Toast.LENGTH_SHORT).show();

    }

并且我的 BrodcastReceiver 类如下:

public class ScheduleHandler extends BroadcastReceiver {

    /* (non-Javadoc)
     * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(context, "Alarm called", Toast.LENGTH_SHORT).show();
    }


}

运行后应用程序中,我只收到第一条 toast 消息。这意味着广播接收器没有被调用。

我的代码有问题吗?

I am implementing a sample code for BroadcastReceiver, where it will start from a specific time, but there is something problem in my code and its not working..

Here is my code:

In Activity:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Calendar cur_cal = new GregorianCalendar(TimeZone.getTimeZone("IST"), Locale.US);
        cur_cal.setTimeInMillis(System.currentTimeMillis());

        Calendar cal = new GregorianCalendar();
        cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
        cal.add(Calendar.HOUR_OF_DAY, 13);
        cal.add(Calendar.MINUTE, 10);
        cal.add(Calendar.SECOND, 00);
        cal.add(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DATE, 31);
        cal.add(Calendar.MONTH, 10);

        Intent intent = new Intent(HelloAlarmManagerActivity.this, ScheduleHandler.class);
        PendingIntent pIntent = PendingIntent.getBroadcast(HelloAlarmManagerActivity.this, 0, intent, 0);

        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 2*10, pIntent);
        Toast.makeText(HelloAlarmManagerActivity.this, "Alarm seted", Toast.LENGTH_SHORT).show();

    }

and my BrodcastReceiver class is as :

public class ScheduleHandler extends BroadcastReceiver {

    /* (non-Javadoc)
     * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(context, "Alarm called", Toast.LENGTH_SHORT).show();
    }


}

After running this application, I am getting only first toast message. It means broadcast receiver not getting called.

Is there any problem in my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

私藏温柔 2024-12-19 12:01:45

您的重复间隔似乎是 20 (2*10) 毫秒。我不知道手机对此有何反应,但这可能是您问题的原因

It looks like your Repeat interval is 20 (2*10) milliseconds. I have no Idea how phones would react to that, but It might be the cause of your problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文