1

イースター、おめでとう

そして、あなたの助けを前もってありがとうございました.

サービスを介して 1 分ごとに更新されるアプリ ウィジェットがあります。

サービスはインターネットからデータを収集するため、ウィジェットの更新に数秒かかります。

問題は、その更新時間中にアプリ ウィジェット自体が数秒間完全に別の画像に置き換えられ (下の画像を参照)、その後通常の状態に戻ることです。

ウィジェットは通常、次のように表示されます。

ここに画像の説明を入力

これは、更新中に何が起こるかです!!!

ここに画像の説明を入力

数秒後、すべてが再び正常になります。

ここに画像の説明を入力

これを回避する方法についての提案は大歓迎です!!

この投稿で誰かが行った方法で、サービスをセルフストップしようとしました: Android ウィジェットが更新中に奇妙な画像を表示する

しかし、成功しませんでした。

これは、アプリ ウィジェットを更新するサービスのコード全体です (あまり役に立たないと思います)。

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;
    }
}

 }

どうもありがとう!!!

4

1 に答える 1