2

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); 
3
  • Should the line String data = getVerseData.getString("mydata"); not be String data = getPrevData.getString("mydata"); instead? Commented Sep 24, 2011 at 20:46
  • Also if you could post the code you're using to send the broadcast just in case there's something up there. Commented Sep 24, 2011 at 20:47
  • Hi i mistyped in stack overflow, sorry my mistake! For more refrence i will do a second update with my complete code. Commented Sep 25, 2011 at 9:09

2 Answers 2

1

You need to use BroadcastReceivers. In a nutshell: add a BroadcastReceiver to your AppWidgetProvider which acts upon a custom Intent 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 the Intent with putExtra as per usual).

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Joseph thank you for the quick answer, im working upon word by word if ur valuable answer. I think i have managed to create a broadcast reciever to my AppWidgtProvider which is Intent myIntent = new Intent(context.getApplicationContext(),MyBroadcastReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, myIntent, 0); Now in my MyBroadcastReciever class what should i do? "acts upon a custom Intent and refreshes widget" Do i need do create an alarm manager? im complete newbie please can you guide me in anyway?
In fact you could just broadcast an Intent with the action android.appwidget.action.APPWIDGET_UPDATE from your activity (and avoid a custom BroadcastReceiver completely). Then override the onReceive method of your AppWidgetProvider, get any extras you want from the intent, and then call super.onReceive which will refresh the widgets.
thanks again :) Please see my Updated Question. according to the changes you have mentioned.
0

You can update the data in widget without using Broadcast receiver.

  1. Save data in sharedPreference.

  2. Create the remote views using new data and use AppWidgetManager.updateWidget to update the widget.

Here is the sample code.

RemoteViews updateViews = buildUpdate(context); // Update the view using the new data. ComponentName thisWidget = new ComponentName(context, WidgetClassName.class); AppWidgetManager manager = AppWidgetManager.getInstance(context); manager.updateAppWidget(thisWidget, updateViews); 

This is what i did in my previous application.

Saneesh CS

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.