android小部件使用活动更新文本

时间:2014-04-22 12:38:15

标签: android buttonclick

我希望在使用WelcomeWidget类onReceive()中的活动添加或删除项目时更新窗口小部件项目

 public void onReceive(Context context, Intent intent) { 
 setup(context); 
 if (datalist.size() != 0)
 { 
 if (intent.getAction().equals(ACTION_NEXT_TIP)) {
     mMessage = getNextMessageIndex();
     SharedPreferences.Editor pref = context.getSharedPreferences( 
      PREFS_NAME,            0).edit(); 
     pref.putInt(PREFS_TIP_NUMBER, mMessage); 
    pref.commit(); 
    refresh();
    } 
    else if (intent.getAction().equals(ACTION_SETTING))
     {
     Intent articleIntent = new Intent(context,
     LoremActivity.class);                 
      articleIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    context.startActivity(articleIntent);      
  } else {

      refresh();        
  }  }   }

其中refresh方法如下:

private void refresh() {

     RemoteViews rv = buildUpdate(mContext); 
     for (int i : mWidgetIds) {
      mWidgetManager.updateAppWidget(i, rv); 
         }
  

将动画用作:

AnimationSet farsiTelLogoAnimation = new AnimationSet(true);
         RotateAnimation rotate = new RotateAnimation(0, 360,
        RotateAnimation.RELATIVE_TO_SELF, 0.5f,
        RotateAnimation.RELATIVE_TO_SELF, 0.5f);      
        rotate.setFillAfter(true);     
       rotate.setDuration(1000);
        farsiTelLogoAnimation.addAnimation(rotate);    

}

获取消息索引       private int getNextMessageIndex(){

  return (mMessage + 1) % datalist.size();

     }

其中buildUpdate()方法为

     public RemoteViews buildUpdate(Context context) { 

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

    // Action for tap on bubble        
       Intent bcast = new Intent(context,
       WelcomeWidget.class);
       bcast.setAction(ACTION_NEXT_TIP);
        PendingIntent pending = PendingIntent.getBroadcast(context, 
          0, bcast, PendingIntent.FLAG_UPDATE_CURRENT); 
      updateViews.setOnClickPendingIntent(R.id.widget, pending);

       // RemoteViews updateViews1 = new
      RemoteViews(context.getPackageName(), // R.id.setting);

       Intent bcast1 = new Intent(context, WelcomeWidget.class);
        bcast1.setAction(ACTION_SETTING);       PendingIntent pending1 =
      PendingIntent.getBroadcast(context, 
          0, bcast1,      PendingIntent.FLAG_UPDATE_CURRENT);       
      updateViews.
      setOnClickPendingIntent(R.id.setting, pending1);

        // Tip bubble text      if (mMessage >= 0) {            // String[] parts =

     sNewlineRegex.split(mTips[mMessage], 2); 
     String to = datalist.get(mMessage).getFrom(); 
     String from =       datalist.get(mMessage).getTo();
      String rate = datalist.get(mMessage).getRate();

  // Look for a callout graphic referenced in the text            Matcher m =

     sDrawableRegex.matcher(to); 
    if (m.find()) {
      String imageName = m.group(1);
      int resId = context.getResources().getIdentifier(

      imageName, null, context.getPackageName());
      // updateViews.setImageViewResource(R.id.tip_callout, resId);
      // updateViews.setViewVisibility(R.id.tip_callout,
      // View.VISIBLE);
      to = m.replaceFirst("");            } else {
      // updateViews.setImageViewResource(R.id.tip_callout, 0);0
      // updateViews.setViewVisibility(R.id.tip_callout, View.GONE);          }

    updateViews.setTextViewText(R.id.to, to);
    updateViews.setTextViewText(R.id.from, from);
    updateViews.setTextViewText(R.id.rate, rate);
    updateViews.setTextViewText(
          R.id.tip_footer,
          context.getResources().getString(R.string.pager_footer,
                  (1 + mMessage), datalist.size()));              
             updateViews.setViewVisibility(R.id.tip_bubble, View.VISIBLE);   

            }
           else { 
                  updateViews.setViewVisibility(R.id.tip_bubble, View.INVISIBLE);
               }

           return updateViews;    

          }

按钮单击事件重新加载小部件

1 个答案:

答案 0 :(得分:0)

我不太了解您的代码,因为格式很糟糕。

无论如何,要从您的活动更新小部件,您可以使用APPWIDGET_UPDATE发送广播意图。使用以下代码:

Intent intent = new Intent(YourActivity.this, YourWidgetProvider.class);
intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
int ids[] = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), ASquareAnalogClockProvider.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
sendBroadcast(intent);

希望有所帮助:)