GVKun编程网logo

android – 使用AppWidgetProvider在Home Widget上显示ListView(android.widget.listview)

25

在这篇文章中,我们将带领您了解android–使用AppWidgetProvider在HomeWidget上显示ListView的全貌,包括android.widget.listview的相关情况。同

在这篇文章中,我们将带领您了解android – 使用AppWidgetProvider在Home Widget上显示ListView的全貌,包括android.widget.listview的相关情况。同时,我们还将为您介绍有关android app widget 中使用自定义 view、android appwidget listview没有更新、Android Appwidget textview不更新、android appwidgetprovider 与 service 交互问题的知识,以帮助您更好地理解这个主题。

本文目录一览:

android – 使用AppWidgetProvider在Home Widget上显示ListView(android.widget.listview)

android – 使用AppWidgetProvider在Home Widget上显示ListView(android.widget.listview)

我想创建一个包含ListView的Home Widget,但我不知道这是否可行,如果可能,该怎么做.
我使用的是ListActivity,它很简单,但无法找到使用appwidgetprovider的方法.

谢谢你的帮助

解决方法:

遗憾的是,这是不可能的,app小部件不支持ListViews.

android app widget 中使用自定义 view

android app widget 中使用自定义 view

app widget 开发中,我想 使用自己定义的 view, 因为 app widget 支持 RemoteViews, 我想问的是 RemoteViews 中怎么样使用自己的 view, 如自定义 view 是 com.my.AwView, 使用代码怎么样加载这个 view 呢?

android appwidget listview没有更新

android appwidget listview没有更新

我编写了一个AppWidget,它从ContentProvider中显示ListView中的一些数据,但是我无法更新它.当我第一次创建窗口小部件时,它会正确填充,但在AlarmManager的PendingIntent到达后,ListView上不会发生更新.这是代码:
Intent update = new Intent(context,MenuWidgetProvider.class);
update.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
update.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,appWidgetIds);
PendingIntent pIUpdate = PendingIntent.getbroadcast(context,update,0);
((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).
set(AlarmManager.RTC,nextTime.toMillis(false),pIUpdate);

Log.d(TAG,"updating: " + dmt.mealName);

for (int i = 0; i < appWidgetIds.length; ++i) {
    int widgetId = appWidgetIds[i];

    RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.widget_menu);
    // meal name
    views.setTextViewText(R.id.widget_locname,prefs.getString(PREF_WIDGET_LOCNAME,""));

    // adapter
    Intent adapter = new Intent(context,MenuWidgetAdapterService.class);
    adapter.putExtra(EXTRA_LOCATIONID,prefs.getInt(PREF_WIDGET_LociD,-1));
    adapter.putExtra(EXTRA_MEALNAME,dmt.mealName);
    adapter.putExtra(EXTRA_DATE,dmt.date.toString());
    views.setRemoteAdapter(R.id.widget_list,adapter);

    // update
    manager.updateAppWidget(widgetId,views);
}

super.onUpdate(context,manager,appWidgetIds);

奇怪的是,当更新发生时,onUpdate()方法运行 – 我看到Log.d调用的输出 – 但我的RemoteViewsFactory中没有调用onDataSetChanged().即使我调用notifyAppWidgetViewDataChanged,也没有效果.

解决方法

这是因为您的RemoteViewsFactory缓存了为每个窗口小部件ID创建的工厂.因此,当相同的小部件发送创建新工厂的请求(通过Widget适配器服务)时,将返回相同的工厂.因此,列表视图不是“更新的”.

我是如何解决它的:

在你的WidgetProviderClass中

adapter.setData(Uri.fromParts("content",String.valueOf(appWidgetIds[i]+randomNumber),null));

randomNumber是一个随机数(WidgetProvider类的公共静态成员).

然后在RemoteViewsFactory类中,执行:

appWidgetId = Integer.valueOf(intent.getData().getSchemeSpecificPart())
            - WidgetProvider.randomNumber;

这让“服务”认为它必须为另一个小部件创建一个Factory,因此创建一个带有更新值的新工厂,该工厂附加到您的小部件.

希望这可以帮助.

Android Appwidget textview不更新

Android Appwidget textview不更新

嗨即时通讯我的 Android小部件有一个非常奇怪的问题,香港专业教育学院看起来很多地方,但我似乎无法弄清楚什么是错的.基本上我在我的小部件中调用pendingintent广播并成功地在onrecivie方法中捕获该意图.

但是在onRecive方法中,当我尝试使用RemoteViews为我的组件设置文本时,文本不会更新,也不会调用任何错误.我在下面附上了我的代码,任何帮助都会很棒.

谢谢,
中号

package com.android.FirstWidget;import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.appwidgetprovider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RemoteViews;

public class Exampleappwidgetprovider extends appwidgetprovider {

    @Override
    public void onReceive(Context context,Intent intent) {
        // Todo Auto-generated method stub
        Log.e("ds",intent.getAction());
        Log.e("f","f");
        if(intent.getAction().contains("1")){

            RemoteViews views =  new RemoteViews(context.getPackageName(),R.layout.wid);
            views.setTextViewText(R.id.textView1,"heyheyhey");
            Log.e("fssss","sssf");
        }
        super.onReceive(context,intent);
    }

    public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            // Create an Intent to launch ExampleActivity
            Intent intent = new Intent(context,Exampleappwidgetprovider.class);
            intent.setAction("1");
            PendingIntent pendingIntent = PendingIntent.getbroadcast(context,intent,0);
            // Get the layout for the App Widget and attach an on-click listener
            // to the button
            RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.wid);
            views.setonClickPendingIntent(R.id.arrowLeft,pendingIntent);
           views.setonClickPendingIntent(R.id.arrowRight,pendingIntent);
//views.set
            // Tell the AppWidgetManager to perform an update on the current app widget
            appWidgetManager.updateAppWidget(appWidgetId,views);

        } 

    }
}

解决方法

你需要在onRecieve方法的末尾添加几行….它应该像这样..

package com.android.FirstWidget;import android.app.PendingIntent;
 import android.appwidget.AppWidgetManager;
 import android.appwidget.appwidgetprovider;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.ImageButton;
 import android.widget.RemoteViews;

 public class Exampleappwidgetprovider extends appwidgetprovider {


     @Override
     public void onReceive(Context context,Intent intent) {
         // Todo Auto-generated method stub
         Log.e("ds",intent.getAction());
         Log.e("f","f");

         RemoteViews views =  new RemoteViews(context.getPackageName(),R.layout.wid);
         if(intent.getAction().contains("arrow_left")){
             views.setTextViewText(R.id.textView1,"left");
             Log.e("fssss","sssf");
         }
         else if(intent.getAction().contains("arrow_right")){
             views.setTextViewText(R.id.textView1,"right");
             Log.e("fssss","sssf");
         }

         else {
             super.onReceive(context,intent);
         }
         ComponentName componentName = new ComponentName(context,Exampleappwidgetprovider.class);
         AppWidgetManager.getInstance(context).updateAppWidget(componentName,views);
     }

    public void onUpdate(Context context,Exampleappwidgetprovider.class);
            intent.setAction("arrow_left");
            PendingIntent pendingIntent = PendingIntent.getbroadcast(context,pendingIntent);

            intent = new Intent(context,Exampleappwidgetprovider.class);
            intent.setAction("arrow_right");
            pendingIntent = PendingIntent.getbroadcast(context,views.setonClickPendingIntent(R.id.arrowRight,views);

        } 

    }
}

此外,虽然我相信你编码的内容应该可以工作,但如果没有,你可以尝试在清单文件中设置按钮的intent操作.它应该是这样的

<intent-filter >
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
        <action android:name="com.android.FirstWidget.arrow_left"/>
        <action android:name="com.android.FirstWidget.arrow_right"/>
</intent-filter>

我想现在应该可以了.我自己偶然发现了这个问题,并在此找到了解决方案:Clickable widgets in android

android appwidgetprovider 与 service 交互问题

android appwidgetprovider 与 service 交互问题

功能描述 :在手机桌面上开发一个时钟widget,每隔一秒刷新一下时间 。

问题描述:想在service中用timer+timertask 控制时钟 ,在appwidgetprovider 中使用这个service 。

我试过的方法 :1.使用startService()方法 , 发现屏幕上的widget不会跳动 。

2使用过bindService方法,但是运行出现错误,不知道是不是widget组件不支持bindservice 。

我已经增删改查四天了,希望有好心的高手能帮我解决这个问题 。

希望能按照我的问题描述 , 给出详细的关键代码 :appwidgetprovider 中如何调用service , service中timer怎么编写,求解答。。。。

如果觉得我说的不明白,可以给我发邮件:CZhaoSCU@gmail.com ,详细交流 。真的十分感谢。

今天关于android – 使用AppWidgetProvider在Home Widget上显示ListViewandroid.widget.listview的分享就到这里,希望大家有所收获,若想了解更多关于android app widget 中使用自定义 view、android appwidget listview没有更新、Android Appwidget textview不更新、android appwidgetprovider 与 service 交互问题等相关知识,可以在本站进行查询。

本文标签: