2013-12-09 21 views
1

喜的朋友enter link description here列表視圖中的應用程序窗口小部件的Android

此鏈接創建列表視圖控件爲我的應用程序,但在這裏我想補充下面的列表視圖按鈕,在點擊將使用打開我的應用程序, 下面的代碼來實現這一點,因爲它適用於我的其他應用程序,但不是這個例子,如何做到這一點。

類widgetprovider

public class WidgetProvider extends AppWidgetProvider { 

    // String to be sent on Broadcast as soon as Data is Fetched 
    // should be included on WidgetProvider manifest intent action 
    // to be recognized by this WidgetProvider to receive broadcast 
    public static final String DATA_FETCHED = "com.wordpress.laaptu.DATA_FETCHED"; 


    /* 
    * this method is called every 30 mins as specified on widgetinfo.xml this 
    * method is also called on every phone reboot from this method nothing is 
    * updated right now but instead RetmoteFetchService class is called this 
    * service will fetch data,and send broadcast to WidgetProvider this 
    * broadcast will be received by WidgetProvider onReceive which in turn 
    * updates the widget 
    */ 
    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
      int[] appWidgetIds) { 

     // which layout to show on widget 
     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), 
       R.layout.widget_layout); 
     Intent informationIntent = new Intent(context,info.class); 
     PendingIntent infoPendingIntent = PendingIntent.getActivity(context,0, informationIntent, 0); 
     remoteViews.setOnClickPendingIntent(R.id.btnSeeMore,infoPendingIntent); 

     final int N = appWidgetIds.length; 
     for (int i = 0; i < N; i++) { 
      Intent serviceIntent = new Intent(context, RemoteFetchService.class); 
      serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 
        appWidgetIds[i]); 
      context.startService(serviceIntent); 
     } 
     super.onUpdate(context, appWidgetManager, appWidgetIds); 
    } 

清單添加了活性

<activity android:name=".info" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.INFO" /> 
      </intent-filter> 
     </activity> 

當點擊它應該打開我的應用程序,該怎麼做,請幫忙,謝謝

finaly我解決什麼我想, 只寫了一個按鈕,點擊代碼的onReceive &它的工作

@Override 
    public void onReceive(Context context, Intent intent) { 
     super.onReceive(context, intent); 
     if (intent.getAction().equals(DATA_FETCHED)) { 
      Log.i("inside action", "DATA_FETCHED"); 
      int appWidgetId = intent.getIntExtra(
        AppWidgetManager.EXTRA_APPWIDGET_ID, 
        AppWidgetManager.INVALID_APPWIDGET_ID); 
      AppWidgetManager appWidgetManager = AppWidgetManager 
        .getInstance(context); 
      RemoteViews remoteViews = updateWidgetListView(context, appWidgetId); 

       Intent informationIntent = new Intent(context,MainActivity.class); 
       PendingIntent infoPendingIntent = PendingIntent.getActivity(context,0, informationIntent, 0); 
       remoteViews.setOnClickPendingIntent(R.id.txtSeeMore,infoPendingIntent); 

      appWidgetManager.updateAppWidget(appWidgetId, remoteViews); 
     } 

} 

回答

1

您需要定義被重寫你的供應商內部的onReceive方法,則需要設置的PendingIntent將如何處理,:

@Override 
public void onReceive(Context context, Intent intent) { 
    super.onReceive(context, intent); 
    if (intent.getAction().equals(ACTION_NAME)) { 
     // Open your activity here. 
    } 
} 

如果您ListView中的每一項都需要不同的額外幫助的onReceive創建正確的意圖,你可以使用一個FillInIntent爲ListView的每個項目,在getViewAt:

// Set the onClickFillInIntent 
final Intent fillInIntent = new Intent(); 
final Bundle extras = new Bundle(); 
extras.putInt(YourWidgetProvider.ACTION_EXTRA_ITEM, event.getId()); // retrieve this in onReceive with intent.getIntExtra(ACTION_EXTRA_ITEM, -1) 
fillInIntent.putExtras(extras); 
rv.setOnClickFillInIntent(R.id.listview_main_layout, fillInIntent); 
return rv; 

另外,不要忘了你的提供商與RemoteViewsService添加到您的清單:

<receiver 
    android:name="com.example.YourWidgetProvider" 
    android:label="@string/widget_name"> 
    <intent-filter> 
     <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
    </intent-filter> 
    <meta-data 
     android:name="android.appwidget.provider" 
     android:resource="@xml/provider_definition_in_xml" /> 
</receiver> 

<service android:name="com.example.YourRemoteViewsService" 
    android:permission="android.permission.BIND_REMOTEVIEWS" 
    android:exported="false" /> 

編輯:

爲了處理上的按鈕點擊主窗口小部件(未在集合視圖),那就更簡單了:

rv.setOnClickPendingIntent(R.id.button_id, PendingIntent.getBroadcast(context, 0, 
         new Intent(context, YourWidgetProvider.class) 
         .setAction(ACTION_NAME), 
         PendingIntent.FLAG_UPDATE_CURRENT)); 

的ACTION_NAME需要從不同當然,因爲它需要生成不同的Intent,所以您用於集合視圖上點擊的名稱。

我希望這有助於!

+0

讓我試試這個 – pitu

+0

您可以檢查http://docs.huihoo.com/android/3.0/resources/samples/StackWidget/index.html或http://www.broculos.net/2011/12/android -101-how-to-create-stackview.html#.UqWUA2TuLnR如果你需要完整實現的例子;) – 2Dee

+0

請你建議我如何在上面提到的鏈接中得到這個,因爲你建議我的方法在其他例如,該鏈接在那裏工作,但沒有, – pitu

相關問題