通过 GetExtras(或)共享首选项获取字符串到 AppWidget

发布于 2024-12-06 18:03:22 字数 1719 浏览 0 评论 0原文

我有一个应用程序,它基本上有一个文本视图。我也有 显示其中创建的字符串的文本视图的活动。现在 我在活动 1 中有一个按钮,onClick 将发送数据(字符串到 appWidget 类)我尝试了 putExtra 和 getExtra 方法以及 Shared 偏好方法,我有点困惑先使用哪个!

为了更清楚起见,以下是我的一些输入。 Activity1:

final String addwidget =
((TextView)findViewById(R.id.verse)).getText().toString();
Intent widgetIntent = new Intent(MyScheduledActivity.this,
MainWidget.class);
widgetIntent.putExtra("widgetVerse", addwidget);

AppWidget:(

public class MainWidget extends AppWidgetProvider {

       RemoteViews views;
       public static String verseFromFav;
       private Bundle getVerseData;

       @Override
       public void onReceive(Context context, Intent intent) {

               getVerseData = intent.getExtras();
               verseFromFav = getVerseData.getString("widgetVerse");
               super.onReceive(context, intent);
       }

       @Override
       public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
          final int N = appWidgetIds.length;
          for (int i=0; i<N; i++) {

          views = new RemoteViews(context.getPackageName(),R.layout.widget_layout);

          if(verseFromFav == null){
               verseFromFav = "no verse";
          }
          views.setTextViewText(R.id.widgetVerse, verseFromFav);

          ComponentName thisWidget = new ComponentName( context, MainWidget.class );

AppWidgetManager.getInstance( context ).updateAppWidget( thisWidget, views );

         appWidgetManager.updateAppWidget(appWidgetIds, views);

         }
         super.onUpdate(context, appWidgetManager, appWidgetIds);
       }
}

应用程序小部件显示“无诗句”,其内容为空) 任何人都可以帮我了解如何显示字符串的基本想法吗 这里。请。提前致谢。

I have an app widged which basically has a textview. I also have
an activity which displays a textview from string created within. Now
i have a button in Activity 1 which onClick will send data (string to
appWidget class) I tried putExtra and getExtra methods and also Shared
preferences method, im little confused to use which first!

Here are some inputs from me for more clarity.
Activity1:

final String addwidget =
((TextView)findViewById(R.id.verse)).getText().toString();
Intent widgetIntent = new Intent(MyScheduledActivity.this,
MainWidget.class);
widgetIntent.putExtra("widgetVerse", addwidget);

AppWidget:

public class MainWidget extends AppWidgetProvider {

       RemoteViews views;
       public static String verseFromFav;
       private Bundle getVerseData;

       @Override
       public void onReceive(Context context, Intent intent) {

               getVerseData = intent.getExtras();
               verseFromFav = getVerseData.getString("widgetVerse");
               super.onReceive(context, intent);
       }

       @Override
       public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
          final int N = appWidgetIds.length;
          for (int i=0; i<N; i++) {

          views = new RemoteViews(context.getPackageName(),R.layout.widget_layout);

          if(verseFromFav == null){
               verseFromFav = "no verse";
          }
          views.setTextViewText(R.id.widgetVerse, verseFromFav);

          ComponentName thisWidget = new ComponentName( context, MainWidget.class );

AppWidgetManager.getInstance( context ).updateAppWidget( thisWidget, views );

         appWidgetManager.updateAppWidget(appWidgetIds, views);

         }
         super.onUpdate(context, appWidgetManager, appWidgetIds);
       }
}

(THE APP WIDGET SHOWS "no verse" WHICH READS NULL)
Can any one help me with a basic idea on how to display the string
here. please. thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筑梦 2024-12-13 18:03:22

onUpdate 将始终被调用(有时多次),因此利用它来触发实用程序或控制器类并完成该类中的所有工作。

因此,在您的应用程序小部件中,您可能拥有的唯一东西(我所演示的只是更新 TextView 中的字符串)是 onUpdate() 方法中的内容。

@Override
public void onUpdate( final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds ) {
    // Need this here to set the initial state of the app widget
    final AppWidgetController _actrlr = new AppWidgetController( context );
    _actrlr.updateAppWidgetUI( this );

    super.onUpdate( context, appWidgetManager, appWidgetIds );
}

然后在 AppWidgetController 类中你可能有这样的东西...

public final void updateAppWidgetUI( final Context context ) {

        final AppWidgetManager _manager = AppWidgetManager.getInstance( context  );
        final ComponentName _componentName = new ComponentName( context, com.your.namespace.theprovidername.class );
        final int[] appWidgetIds = _manager.getAppWidgetIds( _componentName );

        for ( final int i : appWidgetIds ) {

            final RemoteViews _rv = new RemoteViews( context.getPackageName(), R.layout.appwidget_xml_layout_file );

            if( somethingIsTrue ) {
                _rv.setTextViewText( R.id.textViewThatYouWantToUpdate, "The text that you wish to display if something is TRUE" );
            } else {
                _rv.setTextViewText( R.id.textViewThatYouWantToUpdate, "The text that you wish to display if something is FALSE" );
            }

            _manager.updateAppWidget( i, _rv );
        }
    }

RemoteView setTextViewText()

http://developer.android.com/reference/android/widget/RemoteViews.html#setTextViewText(int, java.lang.CharSequence)

onUpdate will ALWAYS be called (sometimes multiple times) so utilize that to fire a utility or controller class and do all the work in that class.

Thus, in your app widget the only thing you might have, all I am demonstrating is updating a string in a TextView, is stuff in the onUpdate() method.

@Override
public void onUpdate( final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds ) {
    // Need this here to set the initial state of the app widget
    final AppWidgetController _actrlr = new AppWidgetController( context );
    _actrlr.updateAppWidgetUI( this );

    super.onUpdate( context, appWidgetManager, appWidgetIds );
}

Then in AppWidgetController class you might have something like this...

public final void updateAppWidgetUI( final Context context ) {

        final AppWidgetManager _manager = AppWidgetManager.getInstance( context  );
        final ComponentName _componentName = new ComponentName( context, com.your.namespace.theprovidername.class );
        final int[] appWidgetIds = _manager.getAppWidgetIds( _componentName );

        for ( final int i : appWidgetIds ) {

            final RemoteViews _rv = new RemoteViews( context.getPackageName(), R.layout.appwidget_xml_layout_file );

            if( somethingIsTrue ) {
                _rv.setTextViewText( R.id.textViewThatYouWantToUpdate, "The text that you wish to display if something is TRUE" );
            } else {
                _rv.setTextViewText( R.id.textViewThatYouWantToUpdate, "The text that you wish to display if something is FALSE" );
            }

            _manager.updateAppWidget( i, _rv );
        }
    }

RemoteView setTextViewText()

http://developer.android.com/reference/android/widget/RemoteViews.html#setTextViewText(int, java.lang.CharSequence)

乖乖 2024-12-13 18:03:22

我建议您为此使用共享首选项。它会对你有更好的帮助。

SharedPreferences settings =  
getSharedPreferences("MyWigdetPreferences", MODE_WORLD_READABLE);  

SharedPreferences.Editor prefEditor = Settings.edit();  
prefEditor.putString("widgetVerse", addwidget);    
prefEditor.commit();  

然后在 mainWidget 活动中将其拉出..

  SharedPreferences settings =  
getSharedPreferences("MyWigdetPreferences", MODE_WORLD_READABLE);  
 String verse = settings.getString("widgetVerse");

应该可以了。

编辑:

而不是

   @Override
   public void onReceive(Context context, Intent intent) {

           getVerseData = intent.getExtras();
           verseFromFav = getVerseData.getString("widgetVerse");
           super.onReceive(context, intent);
   }

尝试

   @Override
   public void onReceive(Context context, Intent intent) {

           getVerseData = getIntent.getExtras();
           verseFromFav = getVerseData.getString("widgetVerse");
           super.onReceive(context, intent);
   }

I would advise you to use sharedpreference for this. It would do you better.

SharedPreferences settings =  
getSharedPreferences("MyWigdetPreferences", MODE_WORLD_READABLE);  

SharedPreferences.Editor prefEditor = Settings.edit();  
prefEditor.putString("widgetVerse", addwidget);    
prefEditor.commit();  

Then in the mainWidget activity pull it out by..

  SharedPreferences settings =  
getSharedPreferences("MyWigdetPreferences", MODE_WORLD_READABLE);  
 String verse = settings.getString("widgetVerse");

That should do it.

EDIT:

Instead of

   @Override
   public void onReceive(Context context, Intent intent) {

           getVerseData = intent.getExtras();
           verseFromFav = getVerseData.getString("widgetVerse");
           super.onReceive(context, intent);
   }

Try

   @Override
   public void onReceive(Context context, Intent intent) {

           getVerseData = getIntent.getExtras();
           verseFromFav = getVerseData.getString("widgetVerse");
           super.onReceive(context, intent);
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文