从另一个活动获取数据到应用程序小部件
您好,我有一个活动 1,需要将数据发送到具有文本视图的 Appwidget(小部件)。
为了在活动之间发送数据,我知道我们可以使用 intent.putExtra("mydata", dataString);
并使用 String data = bundle.getString("mydata");< 接收相同的数据/code> 但就我而言,我需要将数据(字符串)发送到应用程序小部件。
当我在 AppWidgetProvider 内部使用时,
Bundle dataFromPrevious = getIntent().getExtras();
String newString = dataFromPrevious.getString("mydata");
它会在 getIntent 中抛出错误,指出 getIntent 对于类型 Class 未定义。
我怎样才能获取这个AppWidget中的字符串?此外,此文本会经常从活动 1 中使用新字符串进行更新,因此在这种情况下使用 SharedPrefrences 是一个不错的选择吗?还有其他办法吗?
更新 1:
正如约瑟夫提到的,我添加了
在我的清单中,
现在我还在 AppWidget 中创建了 OnRecieve 覆盖
@Override
public void onReceive(Context context, Intent intent) {
Bundle getPrevData = intent.getExtras();
String data = getPrevData.getString("mydata");
newdata = data;
super.onReceive(context, intent);
}
在我的 On Update 中,我
views.setTextViewText(R.id.dataWidget,newdata);
这里的 newdata
是 public static String
但它不显示任何内容! !当我将 TextViewText 设置为小部件中的文本视图时。我在这里错过了什么吗?请帮忙...
更新2:
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RemoteViews;
import android.widget.Toast;
public class MainWidget extends AppWidgetProvider {
private RemoteViews views;
public static String newdata
@Override
public void onReceive(Context context, Intent intent) {
Bundle getPrevData = intent.getExtras();
String data = getPrevData.getString("mydata");
newdata = data;
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
views = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
appWidgetManager.updateAppWidget(appWidgetIds, views);
//views.setTextViewText(R.id.dataWidget,newdata);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
这是我的另一个发送数据的活动:
final String text = ((TextView)findViewById(R.id.textview)).getText().toString();
Intent intent = new Intent(FirstActivity.this, MainWidget.class);
intent.putExtra("mydata", text);
Hi I have an Activity 1 which needs to send data to Appwidget (widget) which has a text view.
For sending data between activites i know that we can use intent.putExtra("mydata", dataString);
and recive the same with String data = bundle.getString("mydata");
But in my case i need to send data (String) to app Widget.
When i use
Bundle dataFromPrevious = getIntent().getExtras();
String newString = dataFromPrevious.getString("mydata");
inside AppWidgetProvider it throws an error in getIntent saying getIntent is undefined for the type Class.
How can i get the string inside this AppWidget? Also this text will be updated from activity 1 with new string often so is using SharedPrefrences
a good choice for this situation? Is there any other way?
UPDATE 1:
As mentioned by Joseph, i have added<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
in my manifest,
Now i have also created an OnRecieve Override in AppWidget
@Override
public void onReceive(Context context, Intent intent) {
Bundle getPrevData = intent.getExtras();
String data = getPrevData.getString("mydata");
newdata = data;
super.onReceive(context, intent);
}
In my On Update i have
views.setTextViewText(R.id.dataWidget,newdata);
Here the newdata
is the public static String
But it dose not display anything!!! when i setTextViewText to a textview in the widget. Am i missing something here? Please help...
UPDATE 2:
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RemoteViews;
import android.widget.Toast;
public class MainWidget extends AppWidgetProvider {
private RemoteViews views;
public static String newdata
@Override
public void onReceive(Context context, Intent intent) {
Bundle getPrevData = intent.getExtras();
String data = getPrevData.getString("mydata");
newdata = data;
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
views = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
appWidgetManager.updateAppWidget(appWidgetIds, views);
//views.setTextViewText(R.id.dataWidget,newdata);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
And this is my Another activityvthat sends data:
final String text = ((TextView)findViewById(R.id.textview)).getText().toString();
Intent intent = new Intent(FirstActivity.this, MainWidget.class);
intent.putExtra("mydata", text);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用
BroadcastReceiver
。简而言之:将BroadcastReceiver
添加到您的AppWidgetProvider
中,它作用于自定义Intent
,然后刷新您的小部件。从您的
Activity
中,您所需要做的就是发送带有自定义意图的广播 (Context.sendBroadcast
)(并且您可以向Intent
添加数据)像平常一样使用putExtra
)。You need to use
BroadcastReceiver
s. In a nutshell: add aBroadcastReceiver
to yourAppWidgetProvider
which acts upon a customIntent
and then refreshes your widgets.From your
Activity
then all you need to do is send a broadcast (Context.sendBroadcast
) with the custom intent (and you can add data to theIntent
withputExtra
as per usual).您可以在不使用广播接收器的情况下更新小部件中的数据。
将数据保存在sharedPreference中。
使用新数据创建远程视图并使用 AppWidgetManager.updateWidget 更新小部件。
这是示例代码。
这就是我在之前的申请中所做的。
萨尼什·CS
You can update the data in widget without using Broadcast receiver.
Save data in sharedPreference.
Create the remote views using new data and use AppWidgetManager.updateWidget to update the widget.
Here is the sample code.
This is what i did in my previous application.
Saneesh CS