Android:警报未触发以从服务更新小部件
我创建了一个小部件来显示当前日期,每 5-10 秒刷新一次(稍后将增加持续时间)。 在 onUpdate 方法中创建警报以创建服务的意图。 创建了一个扩展广播接收器的 onReceive 方法来启动服务,该服务又用日期更新小部件。 当我调用小部件时, onReceive 最初会触发并显示日期。此后警报器不再响起。
1)我包含 onReceive 方法并调用 startservice 是否正确? 2) 我如何知道创建警报已激活且未触发? 3)下面的代码有什么问题?
请帮忙。
谢谢,萨姆
public class WorldCupScores extends AppWidgetProvider {
public static String TAG = "myActivity";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "receiveeeeeeeeeeee");
Toast.makeText(context, "Receiver", Toast.LENGTH_LONG).show();
context.startService(new Intent(context, UpdateService.class));
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// To prevent any ANR timeouts, we perform the update in a service
//context.startService(new Intent(context, UpdateService.class));
AlarmManager alarmManager;
Log.d(TAG, "before intenttttt");
Intent intent = new Intent(context, UpdateService.class);
Log.d(TAG, "afterrrr intenttttt");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
intent, 0);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.add(Calendar.SECOND, 10);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5*1000, pendingIntent);
Toast.makeText(context, "My alarm started", Toast.LENGTH_LONG).show();
Log.d(TAG, "alarmmmmmmmm");
}
public static class UpdateService extends Service {
@Override
public void onStart(Intent intent, int startId) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "Inside Serviceeeeeeeeeeeeeeeeeeeee");
// Build the widget update for today
//RemoteViews updateViews = buildUpdate(this);
RemoteViews updateViews = new RemoteViews(this.getPackageName(),
R.layout.main);
Date date = new Date();
updateViews.setTextViewText(R.id.scores, "Current Time "
+ date);
// Push update for this widget to the home screen
ComponentName thisWidget = new ComponentName(this, WorldCupScores.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
super.onStart(intent, startId);
}
@Override
public IBinder onBind(Intent intent) {
// We don't need to bind to this service
return null;
}
}
}
I have created a widget to display the current date which refreshes every 5-10 seconds (Will be increasing the duration later).
Created an alarm in the onUpdate method to create the intent for the service.
Created an onReceive method extending broadcast receiver) to start the service which in turn updates the widget with the date.
When I invoke the widget onReceive fires initially and displays the date. After that the alarm doesn't fire.
1) Am I right in including an onReceive method and calling startservice?
2) How do I know the create alarm is active and not firing?
3) What is wrong with the following code?
Please help.
Thanks, Sam
public class WorldCupScores extends AppWidgetProvider {
public static String TAG = "myActivity";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "receiveeeeeeeeeeee");
Toast.makeText(context, "Receiver", Toast.LENGTH_LONG).show();
context.startService(new Intent(context, UpdateService.class));
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// To prevent any ANR timeouts, we perform the update in a service
//context.startService(new Intent(context, UpdateService.class));
AlarmManager alarmManager;
Log.d(TAG, "before intenttttt");
Intent intent = new Intent(context, UpdateService.class);
Log.d(TAG, "afterrrr intenttttt");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
intent, 0);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.add(Calendar.SECOND, 10);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5*1000, pendingIntent);
Toast.makeText(context, "My alarm started", Toast.LENGTH_LONG).show();
Log.d(TAG, "alarmmmmmmmm");
}
public static class UpdateService extends Service {
@Override
public void onStart(Intent intent, int startId) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "Inside Serviceeeeeeeeeeeeeeeeeeeee");
// Build the widget update for today
//RemoteViews updateViews = buildUpdate(this);
RemoteViews updateViews = new RemoteViews(this.getPackageName(),
R.layout.main);
Date date = new Date();
updateViews.setTextViewText(R.id.scores, "Current Time "
+ date);
// Push update for this widget to the home screen
ComponentName thisWidget = new ComponentName(this, WorldCupScores.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
super.onStart(intent, startId);
}
@Override
public IBinder onBind(Intent intent) {
// We don't need to bind to this service
return null;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会那么做。在您当前的代码中,永远不会调用
onUpdate()
,因为onReceive()
永远不会链接到超类。如果它没有触发,则它没有激活。而且,由于
onUpdate()
没有被调用,因此您的闹钟永远不会被安排。I wouldn't quite do it that way. In your current code,
onUpdate()
will never be called, becauseonReceive()
never chains to the superclass.If it is not firing, it is not active. And, since
onUpdate()
is not being called, your alarm is never being scheduled.