获得一个字符串通过GetExtras(或)共享prefrences到AppWidget [英] Getting a String to AppWidget via GetExtras (or) Shared prefrences
问题描述
我有widged一个应用程序基本上有一个TextView。我也有
其中显示从内部创建的字符串一个TextView的活动。现在
我在活动一一个按钮的onClick将发送数据(串
appWidget类)我试图putExtra和getExtra方法,也共享
preferences方法,即时通讯有点糊涂了要使用的第一!
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!
下面是从我一些投入更加清晰。
活动1:
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:
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);
}
}
(应用程序widget则显示无诗曰NULL)
任何一个可以帮助我如何显示字符串一个基本思路
这里。请。先谢谢了。
(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.
推荐答案
的onUpdate将永远被称为(有时是多次),因此利用该火一个实用程序或控制器类和做所有的工作在那个类。
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.
因此,在您的应用程序窗口小部件,你可能具有唯一的事情,我的全部证明是一个TextView更新的字符串,是东西在的onUpdate()方法。
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 );
}
然后在AppWidgetController类,你可能有这样的事情...
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 );
}
}
远程视窗setTextViewText()
RemoteView setTextViewText()
的http://developer.android.com/reference/android/widget/RemoteViews.html#setTextViewText(int, java.lang.CharSequence中)
http://developer.android.com/reference/android/widget/RemoteViews.html#setTextViewText(int, java.lang.CharSequence)
这篇关于获得一个字符串通过GetExtras(或)共享prefrences到AppWidget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!