更新使用AlarmManager应用程序插件 [英] Updating app widget using AlarmManager

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

问题描述

我想不是由1.6docs施加30分钟的限制更频繁地更新一个小部件。阅读几乎每一个岗位的左右,而开发者文档,以及各种其它来源之后,我以为我已经到了一个地步,我可以实现它。因此,我试过了,失败了。从那时起,我已经搜罗了更多的论坛和解决方案,我似乎无法得到它更新。

我有一个设置AlarmManager的更新类:

 公共类更新扩展服务{

    @覆盖
    公共无效ONSTART(意向意图,诠释startId){
          字符串currentTemp = Battery.outputTemp;
          字符串currentLevel = Battery.outputLevel;
          字符串currentCard = Battery.outputCard;
          字符串currentInternal = Battery.memory;
          串currentRam =将String.valueOf(Battery.outputRam).substring(0,3)+MB;

          //更改插件的文本
          RemoteViews updateViews =新RemoteViews(
          this.getPackageName(),R.layout.main);
          //更新温度
          updateViews.setTextViewText(R.id.batteryTemp,currentTemp);
          //更新%
          updateViews.setTextViewText(R.id.batteryLevel,currentLevel);
          //更新级别
          updateViews.setTextViewText(R.id.sdCard,currentCard);
          //更新内部存储器
          updateViews.setTextViewText(R.id.internal,currentInternal);
          //更新RAM
          updateViews.setTextViewText(R.id.ram,currentRam);

          组件名thisWidget =新的组件名(本,Widget.class);
          AppWidgetManager经理= AppWidgetManager.getInstance(本);
          manager.updateAppWidget(thisWidget,updateViews);

    }
    @覆盖
    公众的IBinder onBind(意向意图){
        //无需绑定
        返回null;
    }

}
 

这引起了我的onReceive在我的窗口小部件类频繁火(我有一个敬酒时看到它触发),但它不带有意图(敬酒是为了显示这是他们收到的却是空白)。

我不出来(我是一个相对福利局-2个月慢Android开发的),和AP preciate你们有什么见解。

我的继承人小工具类,以供参考:

 公共类的Widget扩展AppWidgetProvider {


    @覆盖
    公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,
            INT [] appWidgetIds){

        AlarmManager alarmManager;
        意向意图=新的意图(背景下,Update.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(上下文,0,
                意图,PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager =(AlarmManager)上下文
                .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);

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

        //更改插件的文本
        RemoteViews updateViews =新RemoteViews(context.getPackageName()
                R.layout.main);
        //更新温度
        updateViews.setTextViewText(R.id.batteryTemp,currentTemp);
        appWidgetManager.updateAppWidget(appWidgetIds,updateViews);
        //更新%
        updateViews.setTextViewText(R.id.batteryLevel,currentLevel);
        appWidgetManager.updateAppWidget(appWidgetIds,updateViews);
        //更新级别
        updateViews.setTextViewText(R.id.sdCard,currentCard);
        appWidgetManager.updateAppWidget(appWidgetIds,updateViews);
        //更新内部存储器
        updateViews.setTextViewText(R.id.internal,currentInternal);
        appWidgetManager.updateAppWidget(appWidgetIds,updateViews);
        //更新RAM
        updateViews.setTextViewText(R.id.ram,currentRam);
        appWidgetManager.updateAppWidget(appWidgetIds,updateViews);
        super.onUpdate(背景下,appWidgetManager,appWidgetIds);

    }

    公共无效的onReceive(上下文的背景下,意图意图){

        super.onReceive(背景下,意图);
        吐司
                .makeText(上下文,intent.getAction()+上下文,
                        Toast.LENGTH_LONG).show();
        捆绑额外= intent.getExtras();
        如果(临时演员!= NULL){
            AppWidgetManager appWidgetManager = AppWidgetManager
                    .getInstance(上下文);
            组件名thisAppWidget =新的组件名(上下文
                    .getPackageName(),Widget.class.getName());
            INT [] appWidgetIds = appWidgetManager
                    .getAppWidgetIds(thisAppWidget);

            的OnUpdate(背景下,appWidgetManager,appWidgetIds);
        }

    }
}
 

解决方案
  

我有一个设置AlarmManager的更新类:

没有,你不知道。 AlarmManager 无处出现在code段。

您的执行的在第二code段引用 AlarmManager 。有问题包括:

  • 您的每一次设定一个新的重复报警的应用程序插件更新

  • 正在设置5秒的频率就报警,这是绝对的疯狂

  • 正在设置5秒的频率对 _WAKEUP 报警,我认为这是理由在某些司法管辖区的逮捕

  • 您有一个毫无意义的的onReceive()方法,甚至忽略了临时吐司

  • 您是假设有可能是在一个操作字符串的意图吐司,但你不指定操作字符串,当你创建意图那你把 PendingIntent 的报警

  • 您code指的是什么我presume是在电池类的静态数据成员,但它是相当可能的这些都是空/空......或者至少他们会是这样,如果你有一个健全的频率的报警

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.

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

}

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).

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

    }
}

解决方案

I have an Update class that sets the AlarmManager:

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

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

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

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

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

  • 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

  • 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天全站免登陆