0

Androidデバイスで使用可能な接続がない場合、notifyAppWidgetViewDataChangedメソッドを呼び出すとAndroidウィジェットがクラッシュしました。

実際、このメソッドを呼び出すと、サーバーからデータをロードするためのhttpリクエストがあります。

このクラッシュの問題を修正するにはどうすればよいですか?

「更新」ボタンをクリックしたときのStackwidgetProviderクラスのコードは次のとおりです。

else if (intent.getAction().equals(REFRESH_LIST_ACTION)) {

        AppWidgetManager mgr = AppWidgetManager.getInstance(context);

        int[] ids = mgr.getAppWidgetIds(new ComponentName(context, StackWidgetProvider.class));

        for(int id : ids) {
            mgr.notifyAppWidgetViewDataChanged(id, R.id.stack_view);
        }

        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);

        int icon = R.drawable.ic_action_lab_white;
        CharSequence tickerText = "Mise à jour des deals ..";
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);

        CharSequence contentTitle = "Mobideals";
        CharSequence contentText = "Mise à jour des deals terminée";
        Intent notificationIntent = new Intent();
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        mNotificationManager.notify(HELLO_ID, notification);
        mNotificationManager.cancel(HELLO_ID);
        Toast.makeText(context, "Mise à jour de la liste des deals ..", Toast.LENGTH_SHORT).show();

    }

そして、これが私のonSetDataChangedメソッドが宣言されたときのStackWidgetServiceクラスのコードです:

public void onDataSetChanged() {
    // This is triggered when you call AppWidgetManager notifyAppWidgetViewDataChanged
    // on the collection view corresponding to this factory. You can do heaving lifting in
    // here, synchronously. For example, if you need to process an image, fetch something
    // from the network, etc., it is ok to do it here, synchronously. The widget will remain
    // in its current state while work is being done here, so you don't need to worry about
    // locking up the widget.

    //http post
    try{
        HttpClient httpclient = new DefaultHttpClient();

        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
        String filtre = settings.getString("filtre", "Tous");           

        HttpPost httppost = null;

        if (filtre.equals("Tous")) {
            httppost = new HttpPost("http://www.myURL/getDeals.php");
        }

        else if (filtre.equals("Maison")) {
            httppost = new HttpPost("http://www.myURL/getDealsMaison.php");
        }

        else if (filtre.equals("Musique")) {
            httppost = new HttpPost("http://www.myURL/getDealsMusique.php");
        }


        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    }catch(Exception e){
        Log.e("log_tag", "Error in http connexion"+e.toString());
    }
    //convert response to string
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        sb = new StringBuilder();
        sb.append(reader.readLine() + "\n");

        String line="0";
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result=sb.toString();
    }catch(Exception e){
        Log.e("log_tag", "Error converting result "+e.toString());
    }

    //parsing data
    String titre, url, site, ancienprix, nouveauprix, temperature, pseudo, categorie, dealtermine, date;

    try{
        jArray = new JSONArray(result);
        if (jArray.length() != 0) {
            mWidgetItems.removeAll(mWidgetItems);
        }
        JSONObject json_data=null;

        SharedPreferences setNbDeal = PreferenceManager.getDefaultSharedPreferences(mContext);
        String nbDeal = setNbDeal.getString("nbDeal", "30");

        int nbDeals = Integer.parseInt(nbDeal);

        for(int i=0;i<nbDeals;i++){
            json_data = jArray.getJSONObject(i);
            titre=json_data.getString("titre");
            url=json_data.getString("url");
            site=json_data.getString("site");
            ancienprix=json_data.getString("ancienprix");
            nouveauprix=json_data.getString("nouveauprix");
            dealtermine=json_data.getString("dealtermine");
            temperature=json_data.getString("temperature");
            pseudo=json_data.getString("pseudo");
            categorie=json_data.getString("categorie");
            date=json_data.getString("date");

            WidgetItem currentItem = new WidgetItem(titre, url, site, ancienprix, nouveauprix, dealtermine, temperature, pseudo, categorie, date);
            mWidgetItems.add(currentItem);

        }
    }
    catch(JSONException e1){
        //Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
    } catch (ParseException e1) {
        e1.printStackTrace();
    }
}
4

0 に答える 0