2013-05-15 28 views
2

我想基於commonsware代碼來實現小部件 'LoremWidget',看到here在Github上小工具單擊不工作?

的問題是,點擊未在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

05-15 23:24:27.648: I/ActivityManager(161): START {flg=0x10000000 cmp=com.demo.widget/.WidgetService bnds=[509,299][547,337]} from pid -1

2.這是正確的方式來實現只是一個簡單的點擊小部件,有沒有其他的方式呢,這個widgetfactory是唯一的方法?

+0

LoremWidget示例在未經修改的情況下使用時是否可以正常工作? – CommonsWare

+0

@CommonsWare是的,它可以工作,它顯示單詞列表,並顯示一個單擊項目列表上的項目 –

回答

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

這是在窗口小部件中只實現一個簡單的點擊的正確方法,有沒有其他的方式呢,這個窗體是唯一的方法嗎?

這是如何處理應用程序窗口小部件中使用的AdapterView中的點擊次數。

+0

謝謝,我理解了有關適配器視圖的部分,糾正它。第二,我不想執行/加載另一個活動來祝酒信息,我想點擊更新小部件本身?這些方法是否足夠,以及/如何更新代碼? –

+0

@AkhilJain:歡迎您隨時使用AppWidgetManager上的'updateAppWidget()'來更新應用部件。 – CommonsWare

+0

該小部件有一些其他的代碼,已經刷新視圖每5/10秒,基於設置計時器,視圖更新(使用'updateAppWidget()'),但我希望它點擊完成後更新,我是對不起,我是小部件開發的新手,小部件不會在'getView'方法中更新任何內容 也在這裏您實際調用另一個活動來使用'PendingIntent.getActivity()'顯示另一個活動,該活動啓動另一個活動,我不想要 –