Android: Broadcastreceiver with Achartengine ,don't display updates

风流意气都作罢 提交于 2019-12-20 04:23:45

问题


I'm trying to display information got from wifi scan with broadcast receiver ,into a bar chart using achartengine. Well i want to have a real time chart ,every time i receive data from onReceive() method ,i update the chart. To achaeive that ,i create the chart objects in the onReceive() method .But i just get a chart with the first result ,there is no update ,although i can see the changes on the wifi scan. I would like to know what is the problem. This is the code that i'm using:

public class WifiChartsRealTime extends Activity {


LinearLayout layout;
List<int[]> values = new ArrayList<int[]>();
//List<ScanResult> classObject = new ArrayList<ScanResult>();
int [ ] [ ] Center_Frequency_2 = {   { 1,2,3,4,5,6,7,8,9,10,11,12,13,14 },
        { 2412, 2417, 2422, 2427, 2432,2437,2442,2447,2452 ,2457,2462,2467,2472,2484},
    };

WifiManager wifi;
Button      enab;
String resultsString ; 
String[] myStringArray;
int aa = 10;
public class Receiver extends BroadcastReceiver{



    @Override
    public void onReceive(Context context, Intent intent) {


        int length ;
        if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)){
             List<ScanResult> results = wifi.getScanResults();
             length=results.size();
             Toast.makeText(getApplicationContext(),
                        "resultsString***************** :"+results , Toast.LENGTH_LONG)
                        .show();

             Toast.makeText(getApplicationContext(),
                        "length1 :"+length , Toast.LENGTH_LONG)
                        .show();

               Log.i("length1",length +"");
                XYMultipleSeriesRenderer renderer = getTruitonBarRenderer(results.size());
                myChartSettings(renderer);
        /* Intent intent1 = ChartFactory.getBarChartIntent(WifiChartsRealTime.this,getTruitonBarDataset(),renderer,Type.DEFAULT);
            startActivity(intent1);*/
        GraphicalView mChartView = ChartFactory.getBarChartView(WifiChartsRealTime.this, getTruitonBarDataset( results), renderer, Type.STACKED);
        layout.addView(mChartView);
      //  mChartView.refreshDrawableState();
        mChartView.repaint();

         }

    }

}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wifi_charts_real_time);
     layout = (LinearLayout) findViewById(R.id.chart);
    ConnectivityManager cxMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
     wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);

    Receiver receiver = new Receiver();
     registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
     registerReceiver(receiver,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}

 private XYMultipleSeriesDataset getTruitonBarDataset( List<ScanResult> classObject) {
       int SERIES_NR  ; //the number of columns to display,
       ArrayList<String> legendTitles = new ArrayList<String>();
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

         Toast.makeText(getApplicationContext(),
                 "Position XYMultipleSeriesDataset:"+classObject , Toast.LENGTH_LONG)
                 .show();
         Toast.makeText(getApplicationContext(),
                 "SIZE XYMultipleSeriesDataset:"+classObject.size() , Toast.LENGTH_LONG)
                 .show();
       // length = classObject.size();


        for (int j=0;j<classObject.size();j++){
            values.add(new int[] { classObject.get(j).level}); //every serie will concern an access point and contain one RSSI
            }

        for (int k = 0; k < classObject.size(); k++) {
             legendTitles.add(classObject.get(k).SSID);//every serie will have a legend the SSID
        }

        //determination des canaux 
        int[] channelsTab = new int[classObject.size()];
        for(int i=0;i<classObject.size();i++){  
            if  (classObject.get(i).frequency /1000 == 2) {
                for (int j =0;j<14;j++)
                { if (Center_Frequency_2[1][j] == classObject.get(i).frequency)
                    channelsTab[i]= Center_Frequency_2[0][j] ;
                }
                }
            }

        //FIN determination des canaux 

        SERIES_NR = classObject.size() ;//number of access point
          for (int i = 0; i < SERIES_NR; i++) {//Loop for every series
                CategorySeries series = new CategorySeries(legendTitles.get(i));
                int[] v = values.get(i);
                int seriesLength = v.length;
                dataset.addSeries(series.toXYSeries());

                           for (int k = 0; k < seriesLength; k++) {
                     Log.i("STOOP SERIES_NR",SERIES_NR+"");
                     Log.i("STOOP ii",i+"");
                     Log.i("STOOP",k+"");
                     Log.i("STOOP",v[k]+"");
                     Log.i("STOOP",v[k] / 10+"");
                    /* XYSeries tempSerie = dataset.getSeriesAt(i);
                        tempSerie.add(i, 40 - v[k] %10);*/

                    switch (v[k] / 10){
                    case -9 :
                        //series.add(classObject[k]);
                        if (v[k] %10 == 0)
                            //series.add(40);
                        {XYSeries tempSerie = dataset.getSeriesAt(i);
                        tempSerie.add(channelsTab[i], 30);
                        tempSerie.addAnnotation(legendTitles.get(i)+":"+v[k], channelsTab[i], 30);
                        }
                        else 
                        //series.add(40 - v[k] %10);
                        {XYSeries tempSerie = dataset.getSeriesAt(i);
                        tempSerie.add(channelsTab[i], 30 - v[k] %10);
                        tempSerie.addAnnotation(legendTitles.get(i)+":"+v[k] , channelsTab[i], 30 - v[k] %10);
                        }   

                        break;
                    case -8 :
                        //series.add(classObject[k]);
                        if (v[k] %10 == 0)
                            //series.add(40);
                        {XYSeries tempSerie = dataset.getSeriesAt(i);
                        tempSerie.add(channelsTab[i], 40);
                        tempSerie.addAnnotation(legendTitles.get(i)+":"+v[k], channelsTab[i], 40);
                        }
                        else 
                        //series.add(40 - v[k] %10);
                        {XYSeries tempSerie = dataset.getSeriesAt(i);
                        tempSerie.add(channelsTab[i], 40 - v[k] %10);
                        tempSerie.addAnnotation(legendTitles.get(i)+":"+v[k], channelsTab[i], 40 - v[k] %10);
                        }   

                        break;
                    case -7 :
                        //series.add(classObject[k]);
                        if (v[k] %10 == 0)
                            {
                            XYSeries tempSerie = dataset.getSeriesAt(i);
                           tempSerie.add(channelsTab[i], 50);
                            tempSerie.addAnnotation(legendTitles.get(i)+":"+v[k], channelsTab[i], 50);

                            }

                        else
                        //series.add(50 - v[k] %10);
                            {XYSeries tempSerie = dataset.getSeriesAt(i);
                           tempSerie.add(channelsTab[i],50 - v[k] %10);
                            tempSerie.addAnnotation(legendTitles.get(i)+":"+v[k], channelsTab[i], 50 - v[k] %10);
                    //  series.toXYSeries().add(7.5,50 - v[k] %10);
                            }

                        break;
                    case -6 :
                        //series.add(classObject[k]);
                        if (v[k] %10 == 0)
                            //series.add(60);
                        {XYSeries tempSerie = dataset.getSeriesAt(i);
                        tempSerie.add(channelsTab[i], 60);
                        tempSerie.addAnnotation(legendTitles.get(i)+":"+v[k], channelsTab[i], 60);

                        }
                        else
                        //series.add(60 - v[k] %10);
                        {XYSeries tempSerie = dataset.getSeriesAt(i);
                        tempSerie.add(channelsTab[i], 60 - v[k] %10);
                        tempSerie.addAnnotation(legendTitles.get(i)+":"+v[k], channelsTab[i], 60 - v[k] %10);

                        }

                        break;

                    case -5 :
                        //series.add(classObject[k]);
                        if (v[k] %10 == 0)
                            //series.add(70);
                        {XYSeries tempSerie = dataset.getSeriesAt(i);
                        tempSerie.add(channelsTab[i], 70);
                        tempSerie.addAnnotation(legendTitles.get(i)+":"+v[k], channelsTab[i], 70);

                        }
                        else
                        //series.add(70 - v[k] %10);
                        {XYSeries tempSerie = dataset.getSeriesAt(i);
                        tempSerie.add(channelsTab[i], 70 - v[k] %10);
                        tempSerie.addAnnotation(legendTitles.get(i)+":"+v[k], channelsTab[i], 70 - v[k] %10);

                        }

                        break;
                    case -4 :
                        //series.add(classObject[k]);
                        if (v[k] %10 == 0)
                            //series.add(80);
                        {XYSeries tempSerie = dataset.getSeriesAt(i);
                        tempSerie.add(channelsTab[i], 80);
                        tempSerie.addAnnotation(legendTitles.get(i)+":"+v[k], channelsTab[i], 80);

                        }

                        else
                        {XYSeries tempSerie = dataset.getSeriesAt(i);
                        tempSerie.add(channelsTab[i], 80 - v[k] %10);
                        tempSerie.addAnnotation(legendTitles.get(i)+":"+v[k], channelsTab[i], 80 - v[k] %10);

                        }


                        break;
                    case -3 :
                        //series.add(classObject[k]);
                        if (v[k] %10 == 0)
                        {XYSeries tempSerie = dataset.getSeriesAt(i);
                        tempSerie.add(channelsTab[i], 90);
                        tempSerie.addAnnotation(legendTitles.get(i)+":"+v[k], channelsTab[i], 90);

                        }
                        else
                        //series.add(90 - v[k] %10);
                        {XYSeries tempSerie = dataset.getSeriesAt(i);
                        tempSerie.add(channelsTab[i], 90 - v[k] %10);
                        tempSerie.addAnnotation(legendTitles.get(i)+":"+v[k], channelsTab[i], 90 - v[k] %10);

                        }

                        break;


                    }


               }



            }
            return dataset;
    }


public XYMultipleSeriesRenderer getTruitonBarRenderer(int length) {
    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
    renderer.setAxisTitleTextSize(10);
    renderer.setChartTitleTextSize(20);
    renderer.setLabelsTextSize(10);
    renderer.setLegendTextSize(10);
    renderer.setMargins(new int[] { 30, 40, 15, 0 });
    for (int k = 0; k < length; k++)
    {   Log.i("SimpleSeriesRenderer" ,k+"");
        SimpleSeriesRenderer r = new SimpleSeriesRenderer();
        r.setColor(Color.rgb((int)(Math.random() * 255), (int)(Math.random() * 255), (int)Math.random() * 255));
        renderer.addSeriesRenderer(r);
    }


    return renderer;
}

@SuppressWarnings("deprecation")
private void myChartSettings(XYMultipleSeriesRenderer renderer) {
    renderer.setChartTitle("Wifi Channels");
    renderer.setXAxisMin(-1);
    renderer.setXAxisMax(16);
    renderer.setYAxisMin(20);
    renderer.setYAxisMax(100);
    renderer.addXTextLabel(1, "1");
    renderer.addXTextLabel(2, "2");
    renderer.addXTextLabel(3, "3");
    renderer.addXTextLabel(4, "4");
    renderer.addXTextLabel(5, "5");
    renderer.addXTextLabel(6, "6");
    renderer.addXTextLabel(7, "7");
    renderer.addXTextLabel(8, "8");
    renderer.addXTextLabel(9, "9");
    renderer.addXTextLabel(10, "10");
    renderer.addXTextLabel(11, "11");
    renderer.addXTextLabel(12, "12");
    renderer.addXTextLabel(13, "13");
    renderer.addXTextLabel(14, "14");

    renderer.addYTextLabel(0, "");
    renderer.addYTextLabel(20, "-100");
    renderer.addYTextLabel(30, "-90");
    renderer.addYTextLabel(40, "-80");
    renderer.addYTextLabel(50, "-70");
    renderer.addYTextLabel(60, "-60");
    renderer.addYTextLabel(70, "-50");
    renderer.addYTextLabel(80, "-40");
    renderer.addYTextLabel(90, "-30");
    renderer.addYTextLabel(100, "");
    renderer.setPanEnabled(true, true);
    renderer.setZoomEnabled(true, true);



    renderer.setApplyBackgroundColor(true);
    renderer.setBackgroundColor(Color.BLACK);
    renderer.setYLabelsAlign(Align.RIGHT);
    renderer.setBarSpacing(0.5);
    renderer.setXTitle("Canal du WiFi");
    renderer.setYTitle("Puissance du signal[dbm]");
    renderer.setShowGrid(true);
    renderer.setGridColor(Color.GRAY);
    renderer.setXLabels(0); // sets the number of integer labels to appear
  renderer.setXLabels((int) 0.5);//25
    renderer.setYLabels(10);
    renderer.setBarWidth(10);// bar width
    renderer.setXLabelsAlign(Align.LEFT);

}






}

回答1:


Well i resolved this problem ,to have a eal time chart (bar chart ) ,i have to change tho my onReceive method like bellow.I hope it will help someone.

public void onReceive(Context context, Intent intent) {
        WifiManager wifiMan=(WifiManager)WifiChartsRealTime.this.getSystemService(Context.WIFI_SERVICE);
          wifiMan.startScan();


        if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)){
             results = wifi.getScanResults();
             length=results.size();


               Log.i("length1",length +"");
                XYMultipleSeriesRenderer renderer = getTruitonBarRenderer(results.size());
                myChartSettings(renderer);




                   layout = (LinearLayout) findViewById(R.id.chart);
                   if (mChartView != null) {
                       layout.removeView(mChartView);
                    }
                   mChartView = ChartFactory.getBarChartView(WifiChartsRealTime.this, getTruitonBarDataset( results), renderer, Type.STACKED); 
                   layout.addView(mChartView, 0, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));


        }



    }`


来源:https://stackoverflow.com/questions/23056144/android-broadcastreceiver-with-achartengine-dont-display-updates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!