Android:警报未触发以从服务更新小部件

发布于 2024-10-25 06:33:46 字数 2956 浏览 1 评论 0原文

我创建了一个小部件来显示当前日期,每 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 技术交流群。

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

发布评论

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

评论(1

是你 2024-11-01 06:33:46

1)我包含 onReceive 方法并调用 startservice 是否正确?

我不会那么做。在您当前的代码中,永远不会调用 onUpdate() ,因为 onReceive() 永远不会链接到超类。

2) 我如何知道创建警报处于活动状态并且未触发?

如果它没有触发,则它没有激活。而且,由于 onUpdate() 没有被调用,因此您的闹钟永远不会被安排。

1) Am I right in including an onReceive method and calling startservice?

I wouldn't quite do it that way. In your current code, onUpdate() will never be called, because onReceive() never chains to the superclass.

2) How do I know the create alarm is active and not firing?

If it is not firing, it is not active. And, since onUpdate() is not being called, your alarm is never being scheduled.

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