我有一个app widged,基本上有一个textview。我也有 一个活动,显示在其中创建的字符串的文本视图。现在 我在Activity 1中有一个按钮,其中onClick将发送数据(字符串为 appWidget类)我尝试了putExtra和getExtra方法以及Shared 偏好方法,我很难混淆使用哪个!
以下是我的一些意见,以便更清晰。 活性1:
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);
}
}
(APP WIDGET显示“没有经文”,读取为空) 任何人都可以帮助我了解如何显示字符串的基本想法 这里。请。提前谢谢。
答案 0 :(得分:0)
我建议你使用共享偏好。它会让你更好。
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);
}
答案 1 :(得分:0)
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)