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);
String data = getVerseData.getString("mydata");not beString data = getPrevData.getString("mydata");instead?