草庐IT

android - 通过 AppWidgetManager 更新我自己的小部件时,电源控制小部件显示了一小会儿,这是什么问题?

coder 2023-11-24 原文

我在通过 AppWidgetManager.updateAppWidget 手动更新我的小部件时遇到了问题。 平台为 Android 2.2。

代码如下:
我在 list 中为现有 Activity 额外声明了小部件:

<receiver android:name=".Widget" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" />
</receiver>

小部件类在 Widget.java 中声明:

public class Widget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    int use_static_ip;
    RemoteViews remoteViews;

    try {
        use_static_ip = Settings.System.getInt(context.getContentResolver(), Settings.System.WIFI_USE_STATIC_IP);
        if (use_static_ip == 0) { //DHCP
            remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_dhcp);
        } else { //static IP
            remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_static);
        }
        Intent call_activity = new Intent(context, StaticIPToggle.class);
        PendingIntent pending_call_activity = PendingIntent.getActivity(context, 0, call_activity, 0);
        remoteViews.setOnClickPendingIntent(R.id.widget_icon, pending_call_activity);
        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    } catch (SettingNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

在现有 Activity 中,我添加了一些行来手动更新小部件:

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
        ComponentName componentName = new ComponentName(getApplicationContext(), Widget.class);
        int[] ids = appWidgetManager.getAppWidgetIds(componentName);
        Intent update_widget = new Intent();
        update_widget.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
        update_widget.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        getApplicationContext().sendBroadcast(update_widget);

但是现在我有这个错误: 如果 widget 处于 onClicked 状态,它会在显示所需的 widget 之前短时间(~0,5sec-1sec)显示 energy-settings-widget。

这里是这个问题的截图(从左到右的顺序):http://i.stack.imgur.com/rHkjw.jpg

首先是左侧图片中的小部件,然后变为中间图片并在 ~0.5-1 秒后停留在那里或变为右侧图片。

很遗憾,我看不到代码中有任何错误,你呢?

希望你能帮我解决这个问题;) 提前致谢, 问候,奥利

最佳答案

我遇到了完全相同的问题。我通过手动更新小部件而不是通过广播,而是直接调用 AppWidgetManager.updateAppWidget(ComponentName, RemoteViews) 解决了这个问题。相反,来自 AsyncTask。

在你的 Widget 类中试试这个:

/**
 * Asynchronously builds views and updates the widget.
 */
private static class UpdateWidgetTask extends AsyncTask<Void, Void, RemoteViews> {
    private AppWidgetManager mAppWidgetManager;
    private Context mContext;

    private UpdateWidgetTask(AppWidgetManager appWidgetManager, Context context) {
        mAppWidgetManager = appWidgetManager;
        mContext = context;
    }

    @Override
    protected RemoteViews doInBackground(Void... params) {
        DebugLog.d(TAG, "Asynchronously updating widget.");
        RemoteViews views = buildUpdate(mContext);
        return views;
    }

    @Override
    protected void onPostExecute(RemoteViews views) {
        ComponentName thisWidget = new ComponentName(mContext, Widget.class);
        mAppWidgetManager.updateAppWidget(thisWidget, views);
    }
}

/**
 * Asynchronously updates all widgets of this type. This is the fastest way to update the widget.
 *
 * @param context The context.
 */
public static void updateWidget(Context context) {
    new UpdateWidgetTask(AppWidgetManager.getInstance(context), context).execute();
}

然后通过从您的 Activity 调用 Widget.updateWidget(this) 进行更新。

关于android - 通过 AppWidgetManager 更新我自己的小部件时,电源控制小部件显示了一小会儿,这是什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4621786/

有关android - 通过 AppWidgetManager 更新我自己的小部件时,电源控制小部件显示了一小会儿,这是什么问题?的更多相关文章

  1. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  2. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  3. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  4. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  5. ruby - 通过 ruby​​ 进程共享变量 - 2

    我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是

  6. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  7. ruby-on-rails - 使用 rails 4 设计而不更新用户 - 2

    我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它​​不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数

  8. ruby - Fast-stemmer 安装问题 - 2

    由于fast-stemmer的问题,我很难安装我想要的任何ruby​​gem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=

  9. ruby-on-rails - Enumerator.new 如何处理已通过的 block ? - 2

    我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m

  10. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

随机推荐