小工具点击不工作?

时间:2013-05-15 17:52:25

标签: android android-widget click commonsware

我正在尝试基于commonsware代码'LoremWidget'实现widget,请参阅Github上的here

问题是点击无效

WidgetUpdate上的

,与WidgetProvider

相同
for (int appWidgetId : appWidgetIds) 
{
 RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget);
 Intent configIntent = new Intent(context, WidgetService.class);
 PendingIntent configPendingIntent =PendingIntent.getActivity(context, 0, configIntent, 0);
 remoteView.setPendingIntentTemplate(R.id.imgNext, configPendingIntent);
 appWidgetManager.updateAppWidget(appWidgetId, remoteView);
}
清单中的

,与Manifest类似

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demo.widget"
    android:versionCode="1"
    android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
     <activity
         android:name=".WidgetConfig"
         android:label="@string/app_name" >
         <intent-filter>
              <action
                android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
         </intent-filter>
     </activity>
    <receiver
        android:name="BasicWidgetProvider">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_info" />
    </receiver>
      <service android:name="WidgetService"
               android:permission="android.permission.BIND_REMOTEVIEWS" />
</application>
</manifest>

WidgetService, similar to WidgetService

public class WidgetService extends RemoteViewsService 
{
    private static final String TAG=WidgetService.class.getName();
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) 
    {
        Log.d(TAG, "inside remoteview");
        return(new WidgetFactory(this.getApplicationContext(),
                                     intent));
    }
}

WidgetFactory getView method as here

public RemoteViews getViewAt(int arg0) 
    {
        Log.d(TAG, "inside the getview at");
        RemoteViews row=new RemoteViews(ctxt.getPackageName(), R.layout.widget);
            row.setViewVisibility(R.id.imgNext, View.INVISIBLE);
        Intent i=new Intent();
        Bundle extras=new Bundle();
        return(row);
    }

Questions:

1. why is widget not updating, no logs are generated infact the only log relevant to my code is

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.demo.widget" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".WidgetConfig" android:label="@string/app_name" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" /> </intent-filter> </activity> <receiver android:name="BasicWidgetProvider"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info" /> </receiver> <service android:name="WidgetService" android:permission="android.permission.BIND_REMOTEVIEWS" /> </application> </manifest>

2。这是在widget中实现一个简单点击的正确方法,还有其他方法吗,这个widgetfactory是唯一的方法吗?

1 个答案:

答案 0 :(得分:1)

您可以考虑阅读the book for these samples,特别是涵盖此示例的章节,因为您可以在那里找到答案。

  

为什么小部件不更新

因为您正在创建一个活动PendingIntent,其Intent指向服务,从我看到的内容开始:

 Intent configIntent = new Intent(context, WidgetService.class);
 PendingIntent configPendingIntent =PendingIntent.getActivity(context, 0, configIntent, 0);
 remoteView.setPendingIntentTemplate(R.id.imgNext, configPendingIntent);

这与what the sample does不匹配。

  

这是在widget中实现一个简单点击的正确方法,还有其他方法吗,这个widgetfactory是唯一的方法吗?

这是您处理应用小部件中使用的AdapterView点击的方式。