通过 GetExtras(或)共享首选项获取字符串到 AppWidget
我有一个应用程序,它基本上有一个文本视图。我也有 显示其中创建的字符串的文本视图的活动。现在 我在活动 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
onUpdate 将始终被调用(有时多次),因此利用它来触发实用程序或控制器类并完成该类中的所有工作。
因此,在您的应用程序小部件中,您可能拥有的唯一东西(我所演示的只是更新 TextView 中的字符串)是 onUpdate() 方法中的内容。
然后在 AppWidgetController 类中你可能有这样的东西...
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.
Then in AppWidgetController class you might have something like this...
RemoteView setTextViewText()
http://developer.android.com/reference/android/widget/RemoteViews.html#setTextViewText(int, java.lang.CharSequence)
我建议您为此使用共享首选项。它会对你有更好的帮助。
然后在 mainWidget 活动中将其拉出..
应该可以了。
编辑:
而不是
尝试
I would advise you to use sharedpreference for this. It would do you better.
Then in the mainWidget activity pull it out by..
That should do it.
EDIT:
Instead of
Try