草庐IT

android - Marshmallow 升级后 appwidget listview 行未更新或加载

coder 2023-11-27 原文

我有一个小部件应用程序,可以在 Android Lollipop 或更低版本的操作系统上正常运行。当我将我的 nexus 5 升级到棉花糖 (android 6.0) 时,我意识到我的小部件在加载自定义行 ListView 时出现问题。 Widget 有一个动态的textView 和一个动态的listView。 textView 完美运行;但 listView 行项目没有希望。当我添加小部件时,行卡在加载阶段。在 Lollipop 或更低版本的设备上运行的代码与在 Marshmallow 上运行的代码完全相同。我相信 Marshmallow 导致了一个我还没有找到的问题。我希望有人告诉我我做错了什么并帮助我解决这个问题。我很感激所有的帮助。谢谢。

WidgetProvider.class:

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        System.out.println("WidgetProvider.onUpdate()");

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        final boolean isMaviKart = MainUtils.getCardType(context).equals(Constants.CARD_TYPE_MAVI);

        try {
            //set widget id
            ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
            int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
            MainUtils.setWidgetID(prefs, allWidgetIds[allWidgetIds.length - 1]);

            for (int i = 0; i < appWidgetIds.length; i++) {
                Intent intentService = new Intent(context, WidgetService.class);

                intentService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
                intentService.setData(Uri.parse(intentService.toUri(Intent.URI_INTENT_SCHEME)));

                RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget);
                widget.setRemoteAdapter(appWidgetIds[i], R.id.list_view, intentService);

                Intent clickIntent = new Intent(context, MainActivity.class);
                PendingIntent clickPI = PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);

                widget.setPendingIntentTemplate(R.id.list_view, clickPI);

                if (isMaviKart) {
                    widget.setTextViewText(R.id.kalanPara, String.valueOf(MainUtils.getTokenPass(prefs)));
                    widget.setTextViewText(R.id.currency_tl, " Geçiş");
                }
                else {
                    widget.setTextViewText(R.id.kalanPara, String.valueOf(MainUtils.getTotalMoney(prefs)));
                    widget.setTextViewText(R.id.currency_tl, " TL");
                }

                appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(context, context.getResources().getString(R.string.error_occured), Toast.LENGTH_SHORT).show();
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }


    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        System.out.println("WidgetProvider.onReceive()");

        String key = intent.getStringExtra(Constants.EXTRA);
        if (key != null)
            Log.e("TEST", key);

    }

WidgetViewsFactory.class:

@Override
    public RemoteViews getViewAt(int position) {

        Intent intent = new Intent();
        Bundle extras = new Bundle();

        rowButton = context.getResources().getIdentifier("widget_button","id", context.getPackageName());
        rowText = context.getResources().getIdentifier("widget_textview","id", context.getPackageName());

        RemoteViews row = new RemoteViews(context.getPackageName(), R.layout.row);

        // set BUTTON NAMES
        row.setTextViewText(rowButton, getFeeTypes(position));


        // simple rows:
        configureRowsInListview(row, position, context.getResources().getColor(R.color.DeepSkyBlue), View.VISIBLE);




        extras.putString(Constants.EXTRA, getEventKey(position));
        intent.putExtras(extras);
        row.setOnClickFillInIntent(rowButton, intent);
        row.setOnClickFillInIntent(rowText, intent);

        return row;
    }

WidgetService.class:

public class WidgetService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return(new WidgetViewsFactory(this.getApplicationContext()));
    }
}

MainActivity.class: (keyWord 在 Marshmallow 上返回 null)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    prefs = PreferenceManager.getDefaultSharedPreferences(this);

    String keyWord = getIntent().getStringExtra(Constants.EXTRA);
    if (keyWord == null) {
        keyWord = "null";
        Log.e("onCreate() ", "No data Arrived yet!");
    }
}

 private void updateWidgetScreen(String updateData, String currencyType) {

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget);
        ComponentName thisWidget = new ComponentName(this, WidgetProvider.class);
        remoteViews.setTextViewText(R.id.kalanPara, updateData);
        remoteViews.setTextViewText(R.id.currency_tl, currencyType);

        Intent intent = new Intent(this, WidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, MainUtils.getWidgetID(prefs));
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        remoteViews.setRemoteAdapter(MainUtils.getWidgetID(prefs), R.id.list_view, intent);

        Intent clickIntent = new Intent(this, MainActivity.class);
        PendingIntent clickPI = PendingIntent.getActivity(this, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        remoteViews.setPendingIntentTemplate(R.id.list_view, clickPI);
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);

        Log.e("updateWidgetScreen", updateData+" updateData - widgetid: "+ MainUtils.getWidgetID(prefs));
    }

ListView行问题:

最佳答案

我发现了我犯的错误。我有一个带有样式为 android:style/Widget.Material.Button 的 Button 的自定义 ListView 。当我删除按钮的样式时,它解决了我的问题。

   <Button
            android:id="@+id/widget_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Button"
            android:layout_marginRight="10dp"
            android:gravity="center"
            android:padding="8dp"
            android:textColor="@color/White"
            **style="@style/MaterialButton"**
            />

<style name="MaterialButton" parent="android:style/Widget.Material.Button">
       <item name="android:background">@drawable/raised_button_background</item>
</style>

关于android - Marshmallow 升级后 appwidget listview 行未更新或加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33317511/

有关android - Marshmallow 升级后 appwidget listview 行未更新或加载的更多相关文章

  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 - 通过 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

  3. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  4. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  5. ruby - 如何在 Lion 上安装 Xcode 4.6,需要用 RVM 升级 ruby - 2

    我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121

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

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

  7. ruby - RuntimeError(自动加载常量 Apps 多线程时检测到循环依赖 - 2

    我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("

  8. ruby-on-rails - 使用 config.threadsafe 时从 lib/加载模块/类的正确方法是什么!选项? - 2

    我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co

  9. ruby - 在不使用 RVM 的情况下在 Mac 上卸载和升级 Ruby - 2

    我最近决定从我的系统中卸载RVM。在thispage提出的一些论点说服我:实际上,我的决定是,我根本不想担心Ruby的多个版本。我只想使用1.9.2-p290版本而不用担心其他任何事情。但是,当我在我的Mac上运行ruby--version时,它告诉我我的版本是1.8.7。我四处寻找如何简单地从我的Mac上卸载这个Ruby,但奇怪的是我没有找到任何东西。似乎唯一想卸载Ruby的人运行linux,而使用Mac的每个人都推荐RVM。如何从我的Mac上卸载Ruby1.8.7?我想升级到1.9.2-p290版本,并且我希望我的系统上只有一个版本。 最佳答案

  10. ruby-on-rails - 从应用程序中自定义文件夹内的命名空间自动加载 - 2

    我们目前正在为ROR3.2开发自定义cms引擎。在这个过程中,我们希望成为我们的rails应用程序中的一等公民的几个类类型起源,这意味着它们应该驻留在应用程序的app文件夹下,它是插件。目前我们有以下类型:数据源数据类型查看我在app文件夹下创建了多个目录来保存这些:应用/数据源应用/数据类型应用/View更多类型将随之而来,我有点担心应用程序文件夹被这么多目录污染。因此,我想将它们移动到一个子目录/模块中,该子目录/模块包含cms定义的所有类型。所有类都应位于MyCms命名空间内,目录布局应如下所示:应用程序/my_cms/data_source应用程序/my_cms/data_ty

随机推荐