• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps AsyncTask put in a widget (AppWidgetProvider)

Shelly22

Newbie
Hi everybody.

I read from my web space from a text file and paste it in TextView.
Everything works.

But now I want the (AppWidgetProvider) in my widget
insert. My widget has a TextView.

So how can I connect a AsyncTask with a AppWidgetProvider?
Tried it. But the app crashes from then.


My MainActivityl.class with AsyncTask it reads. And my AppProviderWidget.class

Please help me. My English is not so good.
But the page is the best.


LG
 
Just to try to understand the problem a little more...

When your task runs, you want it to update text within the activity and a widget.

I noticed you use the same layout mention in your widget and activity. This is normally not the same, so just confirming.

Here is a snippet you can reference as an example in your async task's onPostExecute to update the widget's textview:

AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
//get widget ids for widgets created
ComponentName widget = new ComponentName(context, WIDGET_CLASS_NAME.class);
int[] widgetIds = widgetManager.getAppWidgetIds(widget);
//update text
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.WIDGET_LAYOUT);
remoteViews.setTextViewText(R.id.TEXTVIEW_ID, text);
//refresh widget to show text
widgetManager.updateAppWidget(widgetIds, remoteViews);
 
Hey shredcode.
That's why i love the American Guys. The Guys have plan from Android.

And it work. Thanks you so much.

So i want only a widget without a Activity.

So my Code is now:


[HIGH]
public class UpdateWidgetService extends Service {

public String str;
public String inputLine;
public StringBuilder text;
public BufferedReader in;
public URL url;

@Override
public void onStart(Intent intent, int startId) {


AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

for (int widgetId : allWidgetIds) {
RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(),R.layout.activity_main);
Intent clickIntent = new Intent(this.getApplicationContext(),AppWidget.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,allWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent,PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.button1, pendingIntent);

new DownloadTask().execute();
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();

super.onStart(intent, startId);
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

public class DownloadTask extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... arg0) {

try {
url = new URL("http://shellsapple.funpic.de/test.txt");
in = new BufferedReader(new InputStreamReader(url.openStream()));
inputLine = null;
text = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
str = in.readLine();
text.append(inputLine);
text.append('\n');
}
in.close();
} catch (MalformedURLException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(String result) {

AppWidgetManager widgetManager = AppWidgetManager.getInstance(getApplication());
//get widget ids for widgets created
ComponentName widget = new ComponentName(getApplication(), AppWidget.class);
int[] widgetIds = widgetManager.getAppWidgetIds(widget);
//update text
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.activity_main);
remoteViews.setTextViewText(R.id.textView1, text);
//refresh widget to show text
widgetManager.updateAppWidget(widgetIds, remoteViews);

}
}
}
[/HIGH]And :

[HIGH]
public class AppWidget extends AppWidgetProvider {



@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {

Intent intent = new Intent(context.getApplicationContext(),UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

context.startService(intent);

}
}
[/HIGH]
 
Back
Top Bottom