使用 AlarmManager 更新应用小部件 [英] Updating app widget using AlarmManager

查看:29
本文介绍了使用 AlarmManager 更新应用小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图比 1.6 文档强加的 30 分钟限制更频繁地更新小部件.在阅读了 SO 中的几乎所有帖子、开发人员文档和各种其他来源后,我认为我已经到了可以实现它的地步.所以,我尝试过,但失败了.从那以后,我搜索了更多论坛和解决方案,但似乎无法更新.

I am trying to update a Widget more frequently than the 30 minute restriction imposed by the 1.6docs. After reading nearly every post in SO, and the developer docs, and various other sources, I thought I had got to a point where i could implement it. And so, I tried, and failed. Since then, I have trawled yet more forums and solutions, and I cannot seem to get it to update.

我有一个设置 AlarmManager 的 Update 类:

I have an Update class that sets the AlarmManager:

public class Update extends Service{

    @Override
    public void onStart(Intent intent, int startId) {
          String currentTemp = Battery.outputTemp;
          String currentLevel = Battery.outputLevel;
          String currentCard = Battery.outputCard;
          String currentInternal = Battery.memory;
          String currentRam = String.valueOf(Battery.outputRam).substring(0, 3) + "MB";

          // Change the text in the widget
          RemoteViews updateViews = new RemoteViews( 
          this.getPackageName(), R.layout.main);
          //update temp
          updateViews.setTextViewText(R.id.batteryTemp, currentTemp); 
          //update %
          updateViews.setTextViewText(R.id.batteryLevel, currentLevel);     
          //update level
          updateViews.setTextViewText(R.id.sdCard, currentCard);
          //update internal memory
          updateViews.setTextViewText(R.id.internal, currentInternal);
          //update ram
          updateViews.setTextViewText(R.id.ram, currentRam);

          ComponentName thisWidget = new ComponentName(this, Widget.class);
          AppWidgetManager manager = AppWidgetManager.getInstance(this);
          manager.updateAppWidget(thisWidget, updateViews);

    }
    @Override
    public IBinder onBind(Intent intent) {
        // no need to bind
        return null;
    }

}

这导致我的小部件类中的 onReceive 频繁触发(我有一个 toast 来查看它何时触发),但它没有意图(toast 旨在在收到它们时显示它,但它是空白的).

This has caused my onReceive in my widget class to fire frequently (i have a toast to see when it fires), yet it carries no intent (the toast is meant to display this as they are received but it is blank).

我无法弄清楚(我是一个相对较慢的 2 个月的 android 开发者),并感谢你们的任何见解.

I cannot figure it out (i'm a relative newb-2 months of slow android dev), and appreciate any insight you guys have.

这是我的小部件类以供参考:

heres my widget class for reference:

    public class Widget extends AppWidgetProvider {


    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        AlarmManager alarmManager;
        Intent intent = new Intent(context, Update.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        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);

        String currentTemp = Battery.outputTemp;
        String currentLevel = Battery.outputLevel;
        String currentCard = Battery.outputCard;
        String currentInternal = Battery.memory;
        String currentRam = String.valueOf(Battery.outputRam).substring(0, 3)
                + "MB";

        // Change the text in the widget
        RemoteViews updateViews = new RemoteViews(context.getPackageName(),
                R.layout.main);
        // update temp
        updateViews.setTextViewText(R.id.batteryTemp, currentTemp);
        appWidgetManager.updateAppWidget(appWidgetIds, updateViews);
        // update %
        updateViews.setTextViewText(R.id.batteryLevel, currentLevel);
        appWidgetManager.updateAppWidget(appWidgetIds, updateViews);
        // update level
        updateViews.setTextViewText(R.id.sdCard, currentCard);
        appWidgetManager.updateAppWidget(appWidgetIds, updateViews);
        // update internal memory
        updateViews.setTextViewText(R.id.internal, currentInternal);
        appWidgetManager.updateAppWidget(appWidgetIds, updateViews);
        // update ram
        updateViews.setTextViewText(R.id.ram, currentRam);
        appWidgetManager.updateAppWidget(appWidgetIds, updateViews);
        super.onUpdate(context, appWidgetManager, appWidgetIds);

    }

    public void onReceive(Context context, Intent intent) {

        super.onReceive(context, intent);
        Toast
                .makeText(context, intent.getAction() + context,
                        Toast.LENGTH_LONG).show();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            AppWidgetManager appWidgetManager = AppWidgetManager
                    .getInstance(context);
            ComponentName thisAppWidget = new ComponentName(context
                    .getPackageName(), Widget.class.getName());
            int[] appWidgetIds = appWidgetManager
                    .getAppWidgetIds(thisAppWidget);

            onUpdate(context, appWidgetManager, appWidgetIds);
        }

    }
}

推荐答案

我有一个设置 AlarmManager 的 Update 类:

I have an Update class that sets the AlarmManager:

不,你没有.AlarmManager 在代码片段中没有出现.

No, you don't. AlarmManager appears nowhere in the code snippet.

确实在第二个代码片段中有对 AlarmManager 的引用.存在的问题包括:

You do have a reference to AlarmManager in the second code snippet. Problems there include:

  • 每次应用小部件更新时,您都会设置一个新的重复闹钟

  • You are setting a new repeating alarm every time the app widget updates

你在闹钟上设置了 5 秒的频率,这太疯狂了

You are setting a 5 second frequency on the alarm, which is utter insanity

您在 _WAKEUP 警报上设置了 5 秒频率,我认为这是您在某些司法管辖区被捕的理由

You are setting a 5 second frequency on a _WAKEUP alarm, which I think is grounds for your arrest in some jurisdictions

你有一个毫无意义的onReceive()方法,甚至忽略了临时的Toast

You have a pointless onReceive() method, even ignoring the temporary Toast

您假设 Toast 中的 Intent 上会有一个动作字符串,但是您在创建 Intent 时没有指定动作字符串您在PendingIntent中为警报输入的code>Intent

You are assuming that there will be an action string on the Intent in your Toast, but you do not specify an action string when you create the Intent that you put in the PendingIntent for the alarm

您的代码指的是我认为是 Battery 类上的静态数据成员,但很可能这些都是空/空...或者至少它们是,如果你的闹钟频率正常

Your code refers to what I presume are static data members on a Battery class, but it is rather likely those are all empty/null... or at least they would be, if you had a sane frequency on the alarm

这篇关于使用 AlarmManager 更新应用小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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