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]