問題描述
Android: App Widget trong quá trình cập nhật hiển thị hình ảnh lạ (Android: App Widget during update shows strange images)
Happy Easter
and thank you very much in advance for your help.
I have an App Widget that is updated via a Service every 1 minute.
the Service takes few seconds to update the widget because it collects data from the internet.
The problem is that during that update time the App Widget itself for a few seconds is replaced by a completely different image (please see images below) then it returns to its normal state.
This is how the widget looks normally:
This is what happens during the update !!!
After few seconds everything is fine again:
Please any suggestion on how to avoid this very welcome!!
I have tried to SelfStop the Service the way somebody did in this post: Android Widget shows strange image during update
but without success.
This is the entire code of the Service that updates the App Widget (I doubt it will be of much help)
public class UpdateService extends Service {
public RemoteViews updateViews;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("", "onStartCommand di AppWidget");
int[] appWidgetIds = intent.getIntArrayExtra("widgetsids");
final int N = appWidgetIds.length;
AppWidgetManager manager = AppWidgetManager.getInstance(this);
// Perform this loop procedure for each App Widget that belongs to
// this provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
Log.e("", "i=" + Integer.toString(i) + " di " + Integer.toString(N));
RemoteViews view = buildUpdate(getApplicationContext(),
appWidgetIds);
// Tell the AppWidgetManager to perform an update on the current
// app widget
manager.updateAppWidget(appWidgetId, view);
}
stopSelf();
Log.e("","stoopo il service");
return (START_STICKY);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto‑generated method stub
return null;
}
private RemoteViews buildUpdate(Context ctxt, int[] appWidgetIds) {
updateViews = new RemoteViews(ctxt.getPackageName(), R.layout.widget);
Log.e("", "invoco buildbpdate");
Intent intent = new Intent(ctxt, AppWidgetConfigure.class);
PendingIntent pendingIntent = PendingIntent.getActivity(ctxt, 0,
intent, 0);
updateViews.setOnClickPendingIntent(R.id.Widgetbackground,
pendingIntent);
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
ArrayList<Stock> list = db.getAllStocks();
if (!list.isEmpty())
Collections.sort(list, new Comparator<Stock>() {
public int compare(Stock s1, Stock s2) {
return s1.getName().compareToIgnoreCase(s2.getName());
}
});
Log.e("", "inizio ad aggiornare");
// PRIMO
if (list.size() >= 1) {
String nome = list.get(0).getName();
if (nome.length() >= 12)
nome = nome.substring(0, 12);
updateViews.setTextViewText(R.id.name1, nome);
Log.e("list.get(1).getName()", list.get(0).getName());
UpdateTaskPrice up = new UpdateTaskPrice();
up.execute(list.get(0).getSymbol(), null, null);
String res = "";
try {
res = up.get();
} catch (InterruptedException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
}
Log.e("risultato di get è ", res);
updateViews.setTextViewText(R.id.price1, res);
UpdateTaskChange chn = new UpdateTaskChange();
chn.execute(list.get(0).getSymbol(), null, null);
String reschn = "";
try {
reschn = chn.get();
} catch (InterruptedException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
}
Log.e("risultato di get è ", reschn);
updateViews.setTextViewText(R.id.change1, reschn+"%");
if (Float.valueOf(reschn) < 0) {
updateViews.setTextViewText(R.id.arrow1, "\u25BC");
updateViews.setTextColor(R.id.change1, Color.RED);
updateViews.setTextColor(R.id.arrow1, Color.RED);
}
if (Float.valueOf(reschn) > 0) {
updateViews.setTextViewText(R.id.arrow1, "\u25B2");
updateViews.setTextColor(R.id.change1, Color.GREEN);
updateViews.setTextColor(R.id.arrow1, Color.GREEN);
}
}
Log.e("", "faccio primo giro di aggiornamento");
// SECONDO
if (list.size() >= 2) {
String nome = list.get(1).getName();
if (nome.length() >= 12)
nome = nome.substring(0, 12);
updateViews.setTextViewText(R.id.name2, nome);
UpdateTaskPrice up = new UpdateTaskPrice();
up.execute(list.get(1).getSymbol(), null, null);
String res = "";
try {
res = up.get();
} catch (InterruptedException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
}
Log.e("risultato di get è ", res);
updateViews.setTextViewText(R.id.price2, res);
UpdateTaskChange chn = new UpdateTaskChange();
chn.execute(list.get(1).getSymbol(), null, null);
String reschn = "";
try {
reschn = chn.get();
} catch (InterruptedException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
}
Log.e("risultato di get è ", reschn);
updateViews.setTextViewText(R.id.change2, reschn+"%");
if (Float.valueOf(reschn) < 0) {
updateViews.setTextViewText(R.id.arrow2, "\u25BC");
updateViews.setTextColor(R.id.change2, Color.RED);
updateViews.setTextColor(R.id.arrow2, Color.RED);
}
if (Float.valueOf(reschn) > 0) {
updateViews.setTextViewText(R.id.arrow2, "\u25B2");
updateViews.setTextColor(R.id.change2, Color.GREEN);
updateViews.setTextColor(R.id.arrow2, Color.GREEN);
}
}
// TERZO
if (list.size() >= 3) {
String nome = list.get(2).getName();
if (nome.length() >= 12)
nome = nome.substring(0, 12);
updateViews.setTextViewText(R.id.name3, nome);
UpdateTaskPrice up = new UpdateTaskPrice();
up.execute(list.get(2).getSymbol(), null, null);
String res = "";
try {
res = up.get();
} catch (InterruptedException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
}
Log.e("risultato di get è ", res);
updateViews.setTextViewText(R.id.price3, res);
UpdateTaskChange chn = new UpdateTaskChange();
chn.execute(list.get(2).getSymbol(), null, null);
String reschn = "";
try {
reschn = chn.get();
} catch (InterruptedException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
}
Log.e("risultato di get è ", reschn);
updateViews.setTextViewText(R.id.change3, reschn+"%");
if (Float.valueOf(reschn) < 0) {
updateViews.setTextViewText(R.id.arrow3, "\u25BC");
updateViews.setTextColor(R.id.change3, Color.RED);
updateViews.setTextColor(R.id.arrow3, Color.RED);
}
if (Float.valueOf(reschn) > 0) {
updateViews.setTextViewText(R.id.arrow3, "\u25B2");
updateViews.setTextColor(R.id.change3, Color.GREEN);
updateViews.setTextColor(R.id.arrow3, Color.GREEN);
}
}
// QUARTO
if (list.size() >= 4) {
String nome = list.get(3).getName();
if (nome.length() >= 12)
nome = nome.substring(0, 12);
updateViews.setTextViewText(R.id.name4, nome);
UpdateTaskPrice up = new UpdateTaskPrice();
up.execute(list.get(3).getSymbol(), null, null);
String res = "";
try {
res = up.get();
} catch (InterruptedException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
}
Log.e("risultato di get è ", res);
updateViews.setTextViewText(R.id.price4, res);
UpdateTaskChange chn = new UpdateTaskChange();
chn.execute(list.get(3).getSymbol(), null, null);
String reschn = "";
try {
reschn = chn.get();
} catch (InterruptedException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
}
Log.e("risultato di get è ", reschn);
updateViews.setTextViewText(R.id.change4, reschn+"%");
if (Float.valueOf(reschn) < 0) {
updateViews.setTextViewText(R.id.arrow4, "\u25BC");
updateViews.setTextColor(R.id.change4, Color.RED);
updateViews.setTextColor(R.id.arrow4, Color.RED);
}
if (Float.valueOf(reschn) > 0) {
updateViews.setTextViewText(R.id.arrow4, "\u25B2");
updateViews.setTextColor(R.id.change4, Color.GREEN);
updateViews.setTextColor(R.id.arrow4, Color.GREEN);
}
}
// QUINTO
if (list.size() >= 5) {
String nome = list.get(4).getName();
if (nome.length() >= 12)
nome = nome.substring(0, 12);
updateViews.setTextViewText(R.id.name5, nome);
UpdateTaskPrice up = new UpdateTaskPrice();
up.execute(list.get(4).getSymbol(), null, null);
String res = "";
try {
res = up.get();
} catch (InterruptedException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
}
Log.e("risultato di get è ", res);
updateViews.setTextViewText(R.id.price5, res);
UpdateTaskChange chn = new UpdateTaskChange();
chn.execute(list.get(4).getSymbol(), null, null);
String reschn = "";
try {
reschn = chn.get();
} catch (InterruptedException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto‑generated catch block
e.printStackTrace();
}
Log.e("risultato di get è ", reschn);
updateViews.setTextViewText(R.id.change5, reschn+"%");
if (Float.valueOf(reschn) < 0) {
updateViews.setTextViewText(R.id.arrow5, "\u25BC");
updateViews.setTextColor(R.id.change5, Color.RED);
updateViews.setTextColor(R.id.arrow5, Color.RED);
}
if (Float.valueOf(reschn) > 0) {
updateViews.setTextViewText(R.id.arrow5, "\u25B2");
updateViews.setTextColor(R.id.change5, Color.GREEN);
updateViews.setTextColor(R.id.arrow5, Color.GREEN);
}
}
return (updateViews);
}
public class UpdateTaskPrice extends AsyncTask<String, String, String> {
public String res;
@Override
protected void onProgressUpdate(String... progress) {
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected String doInBackground(String... symbol) {
Log.e("", "Passo per doinbabground");
String result = "";
DefaultHttpClient client = new DefaultHttpClient();
String srt = "";
String url = getApplicationContext().getString(
R.string.urlaternativo).concat(symbol[0]);
HttpGet getMethod = new HttpGet(url);
try {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
srt = client.execute(getMethod, responseHandler);
int inizio = srt.indexOf("<last data=\"");
int fine = srt.indexOf("\"/>", inizio + 12);
result = srt.substring(inizio + 12, fine);
} catch (Throwable t) {
// Log.e("ERROR", "ERROR", t);
}
Log.e("", "finisco per doinbabground");
return result;
}
}
public class UpdateTaskChange extends AsyncTask<String, String, String> {
public String res;
@Override
protected void onProgressUpdate(String... progress) {
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected String doInBackground(String... symbol) {
Log.e("", "Passo per doinbabground");
String result = "";
DefaultHttpClient client = new DefaultHttpClient();
String srt = "";
String url = getApplicationContext().getString(
R.string.urlaternativo).concat(symbol[0]);
HttpGet getMethod = new HttpGet(url);
try {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
srt = client.execute(getMethod, responseHandler);
int inizio = srt.indexOf("<perc_change data=\"");
int fine = srt.indexOf("\"/>", inizio + 19);
result = srt.substring(inizio + 19, fine);
} catch (Throwable t) {
// Log.e("ERROR", "ERROR", t);
}
Log.e("", "finisco per doinbabground");
return result;
}
}
}
Thanks a Lot!!!
參考解法
方法 1:
In AppWidgetProvider
have method onReceiver
to receiver broadcast action. From change data of service, you sendbroadcast
with data change to widget. Register receiver this broadcast for AppWidgetProvider
in AndroidManifest
. Class extends AppWidgetProvider
will receive change data and widget update it'Ui by it self method onUpdate(....)
(by Lisa Anne、cuasodayleo)