使用AlarmManager和IntentService每秒更新一个小部件

时间:2015-06-08 17:42:17

标签: android widget alarmmanager

我搜索了很多内容并尝试了很多变化,但我无法让我的代码正常工作。我有一个需要每秒更新的小部件,但只有在屏幕解锁时才会更新。

小部件本身应该做两件事:

  1. 设置onClickListener以将用户发送到我的MainActivity
  2. 每秒编辑一个文本框
  3. 由于intervall非常小,只需一秒钟,我认为我必须使用AlarmManger和IntentService进行更新。 Elapsed_Realtime应该是正确的,因为我不想在锁定时唤醒手机。所以这就是我的

      

    AppWidgetProvider.onUpdate(上下文上下文,AppWidgetManager   appWidgetManager,int [] appWidgetIds)

    看起来像:

        @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    
        final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        final Intent intent = new Intent(context, UpdateService.class);
        final PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
    
        alarmManager.cancel(pendingIntent);
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),1000, pendingIntent);
    }
    

    我真的需要在这里使用widget-ID循环吗?

    这就是我的UpdateService的样子:

    public class UpdateService extends IntentService {
    
        public static int testInt = 0;
    
        public UpdateService() {
            super("stuff UpdateService");
        }
    
        @Override
        protected void onHandleIntent(Intent intent) {
    
            RemoteViews remoteViews = new RemoteViews(this.getPackageName(), R.layout.my_widget);
    
            //stuff
            remoteViews.setTextViewText(R.id.textView,String.format("%02d",variableThatChangesEverySecond));
            // more stuff
    
    //set onClickListener
            final Intent openApp = new Intent(this, MainActivity.class);
            final PendingIntent pendingAppIntent = PendingIntent.getActivity(this, 0, openApp, 0);
            remoteViews.setOnClickPendingIntent(R.id.myRelativeLayout, pendingAppIntent);
    
            ComponentName thisWidget = new ComponentName(this,myWidget.class);
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
    
    //log a testInt to see how often my widget gets updates
            testInt = testInt + 1;
            System.out.println(testInt);
    
            appWidgetManager.updateAppWidget(thisWidget, remoteViews);
        }
    }
    

    虽然我设置我的intervall 1000ms = 1s我的小部件更新不准确,只在一两分钟内更新一次。这是查看updateIntervalls的logcat:

    06-08 19:21:10.444  19376-19749/mypackage.myapp I/System.out﹕ 1
    06-08 19:22:42.900  19376-19798/mypackage.myapp I/System.out﹕ 2
    06-08 19:23:02.449  19376-19807/mypackage.myapp I/System.out﹕ 3
    06-08 19:24:43.035  19376-19860/mypackage.myapp I/System.out﹕ 4
    06-08 19:25:43.087  19376-19876/mypackage.myapp I/System.out﹕ 5
    06-08 19:26:43.149  19376-19905/mypackage.myapp I/System.out﹕ 6
    06-08 19:27:43.214  19376-19936/mypackage.myapp I/System.out﹕ 7
    06-08 19:28:43.282  19376-19960/mypackage.myapp I/System.out﹕ 8
    06-08 19:29:43.290  19376-19986/mypackage.myapp I/System.out﹕ 9
    06-08 19:30:43.355  19376-20051/mypackage.myapp I/System.out﹕ 10
    06-08 19:31:43.419  19376-20096/mypackage.myapp I/System.out﹕ 11
    

    我的错误在哪里?非常感谢你,你是我最后的希望......

0 个答案:

没有答案