广播接收器不工作:Android
我正在为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的重复间隔似乎是 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