安卓:报警不费一枪从服务更新的Widget [英] Android: Alarm not firing to update Widget from Service

查看:201
本文介绍了安卓:报警不费一枪从服务更新的Widget的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个小工具来显示的刷新间隔为5-10秒当前日期(稍后会增加持续时间)。
在的onUpdate方法所创建的警报来创建该服务的意图。
创建延伸广播接收机)开始依次与日期更新插件的服务中的onReceive方法。
当我最初调用控件的onReceive火灾和显示日期。在这以后,报警不火。

1)我说的对中包括的onReceive方法和调用startService?
2)我怎么知道创建报警被激活,而不是射击?
3)什么是错的下列code?

请帮忙。

谢谢,山姆

 公共类WorldCupScores扩展AppWidgetProvider {
    公共静态字符串标记=myActivity;    @覆盖
      公共无效的onReceive(上下文的背景下,意图意图){
        Log.d(TAGreceiveeeeeeeeeeee);
        Toast.makeText(背景下,接收器,Toast.LENGTH_LONG).show();
        context.startService(新意图(上下文,UpdateService.class));
          super.onReceive(背景下,意图);
      }      @覆盖
      公共无效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,
          INT [] appWidgetIds){
          //为prevent任何ANR超时,我们在服务执行更新
          //context.startService(new意图(上下文,UpdateService.class));
          AlarmManager alarmManager;
          Log.d(TAGintenttttt之前);
          意向意图=新意图(背景下,UpdateService.class);
          Log.d(TAGafterrrr intenttttt);
          的PendingIntent的PendingIntent = PendingIntent.getBroadcast(上下文,0,
                意图,0);
          alarmManager =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
          日历CAL = Calendar.getInstance();
          cal.setTimeInMillis(System.currentTimeMillis的());
          cal.add(Calendar.SECOND,10);
          alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),5 * 1000,的PendingIntent);
          Toast.makeText(背景下,我的报警开始,Toast.LENGTH_LONG).show();
          Log.d(TAGalarmmmmmmmm);
      }      公共静态类UpdateService延伸服务{
          @覆盖
          公共无效调用onStart(意向意图,诠释startId){            Toast.makeText(这一点,我的服务启动,Toast.LENGTH_LONG).show();
            Log.d(TAG,内部Serviceeeeeeeeeeeeeeeeeeeee);
            //建立今天的小窗口更新
              // RemoteViews updateViews = buildUpdate(本);
            RemoteViews updateViews =新的RemoteViews(this.getPackageName()
                    R.layout.main);            日期日期=新的日期();            updateViews.setTextViewText(R.id.scores,当前时间
                    +日期);              //推送更新这个小部件到主屏幕
              组件名thisWidget =新的组件名(这一点,WorldCupScores.class);
              AppWidgetManager经理= AppWidgetManager.getInstance(本);
              manager.updateAppWidget(thisWidget,updateViews);
              super.onStart(意向,startId);
          }
          @覆盖
          公众的IBinder onBind(意向意图){
              //我们不需要绑定到该服务
              返回null;
          }
      }
  }


解决方案

  

1)我说的对中包括的onReceive方法和调用startService?


我也不太这样做的。在当前的code,的onUpdate()将永远不会被调用,因为的onReceive()从不链的超类。


  

2)我怎么知道创建报警被激活,而不是射击?


如果它不点火,它是不活动。而且,由于的onUpdate()不会被调用,你的闹钟是永远不会被调度。

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;
          }
      }
  }

解决方案

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.

这篇关于安卓:报警不费一枪从服务更新的Widget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆