Getting a String to AppWidget via GetExtras (or) Shared prefrences

落爺英雄遲暮 提交于 2020-01-03 02:42:04

问题


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.


回答1:


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)




回答2:


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);
   }


来源:https://stackoverflow.com/questions/7560265/getting-a-string-to-appwidget-via-getextras-or-shared-prefrences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!