BroadcastReceiver无法正常工作:Android

时间:2011-10-31 08:30:22

标签: android alarmmanager

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

这是我的代码:

活动:

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消息。这意味着广播接收器不被调用。

我的代码有问题吗?

1 个答案:

答案 0 :(得分:1)

看起来你的重复间隔是20(2 * 10)毫秒。我不知道手机会对此做出怎样的反应,但这可能是你问题的原因