Обновление виджета при нажатии на него
Код:
package com.lieryan.urlimagewidget;
import com.lieryan.urlimagewidget.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
public class URLImageAppWidgetProvider extends AppWidgetProvider {
public static String TAG = "Widget";
//
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
//
public static class Size_1_1 extends URLImageAppWidgetProvider {}
public static class Size_1_2 extends URLImageAppWidgetProvider {}
public static class Size_2_2 extends URLImageAppWidgetProvider {}
public static class Size_4_2 extends URLImageAppWidgetProvider {}
public static class Size_4_3 extends URLImageAppWidgetProvider {}
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (ACTION_WIDGET_RECEIVER.equals(action)) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), URLImageAppWidgetProvider.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
this.onUpdate(context, appWidgetManager, appWidgetIds);
}
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
SharedPreferences urls = context.getSharedPreferences("urls.conf", Context.MODE_PRIVATE);
for (int id : appWidgetIds) {
String url = urls.getString("url_" + id, "");
update(context, appWidgetManager, id, url);
//
//Создаем новый RemoteViews
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
//Подготавливаем Intent для Broadcast
Intent active = new Intent(context, URLImageAppWidgetProvider.class);
active.setAction(ACTION_WIDGET_RECEIVER);
active.putExtra("msg", "Запущен!");
//создаем наше событие
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
//регистрируем наше событие
remoteViews.setOnClickPendingIntent(R.id.img, actionPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
SharedPreferences urls = context.getSharedPreferences("urls.conf", Context.MODE_PRIVATE);
SharedPreferences.Editor urls_editor = urls.edit();
for (int id : appWidgetIds) {
urls_editor.remove("url_" + id);
}
urls_editor.commit();
}
public static void update(final Context context, final AppWidgetManager appWidgetManager, final int id, final String url) {
new Thread() {
public void run() {
Bitmap img = getBitmapFromUrl(url);
if (img != null) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
views.setImageViewBitmap(R.id.img, img);
appWidgetManager.updateAppWidget(id, views);
}
}
}.start();
}
private static Bitmap getBitmapFromUrl(final String url) {
try {
return BitmapFactory.decodeStream(((java.io.InputStream)new java.net.URL(url).getContent()));
} catch (Exception e) {
return null;
}
}
}
import com.lieryan.urlimagewidget.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
public class URLImageAppWidgetProvider extends AppWidgetProvider {
public static String TAG = "Widget";
//
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
//
public static class Size_1_1 extends URLImageAppWidgetProvider {}
public static class Size_1_2 extends URLImageAppWidgetProvider {}
public static class Size_2_2 extends URLImageAppWidgetProvider {}
public static class Size_4_2 extends URLImageAppWidgetProvider {}
public static class Size_4_3 extends URLImageAppWidgetProvider {}
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (ACTION_WIDGET_RECEIVER.equals(action)) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), URLImageAppWidgetProvider.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
this.onUpdate(context, appWidgetManager, appWidgetIds);
}
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
SharedPreferences urls = context.getSharedPreferences("urls.conf", Context.MODE_PRIVATE);
for (int id : appWidgetIds) {
String url = urls.getString("url_" + id, "");
update(context, appWidgetManager, id, url);
//
//Создаем новый RemoteViews
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
//Подготавливаем Intent для Broadcast
Intent active = new Intent(context, URLImageAppWidgetProvider.class);
active.setAction(ACTION_WIDGET_RECEIVER);
active.putExtra("msg", "Запущен!");
//создаем наше событие
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
//регистрируем наше событие
remoteViews.setOnClickPendingIntent(R.id.img, actionPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
SharedPreferences urls = context.getSharedPreferences("urls.conf", Context.MODE_PRIVATE);
SharedPreferences.Editor urls_editor = urls.edit();
for (int id : appWidgetIds) {
urls_editor.remove("url_" + id);
}
urls_editor.commit();
}
public static void update(final Context context, final AppWidgetManager appWidgetManager, final int id, final String url) {
new Thread() {
public void run() {
Bitmap img = getBitmapFromUrl(url);
if (img != null) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
views.setImageViewBitmap(R.id.img, img);
appWidgetManager.updateAppWidget(id, views);
}
}
}.start();
}
private static Bitmap getBitmapFromUrl(final String url) {
try {
return BitmapFactory.decodeStream(((java.io.InputStream)new java.net.URL(url).getContent()));
} catch (Exception e) {
return null;
}
}
}