Domanda

I'm trying to pass an array list containing Wi-Fi informations from a class that exdends from BroadcastReceiver to another class .I can pass a normal vriable between two classes with the getter. But i'm getting an empty array, because the onReceive methode is not executed .I would like to execute the onReceive methode in another class(but i can't instantiate it ) or to get the array created in the onReceive methode in another class with a getter (but i cant get the list).Please how can i get this array in the second class.

This is the WifiData class

public class WifiData extends BroadcastReceiver{
List<String[]> wifiValues = new ArrayList<String[]>();
WifiManager wifi;
Button      enab;
String resultsString ; 
String[] myStringArray;
int aa = 10;
 int a=10 ;
 List<String[]>  getWifi (){
    return wifiValues ;
}

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


    if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)){
        List<ScanResult> results = wifi.getScanResults();

        resultsString = results.toString() ;


        for(int i=0;i<results.size();i++){

            Log.i("Wifi SSID",results.get(i).level+"");
            wifiValues.add(new String[] { results.get(i).level +"" });
        }

     }

}

}

This is SalesStackedBarChart class that have to get the wifi infomation and create a chart :

    public class SalesStackedBarChart extends AbstractDemoChart {

  public Intent execute(Context context) {

      WifiData wi = new WifiData ();

    //  values.add(new double[] {wi.getWifi() });
      Log.i("aaaa",wi.getWifi()+"");
     }
} 

Thank you.

È stato utile?

Soluzione

You are trying to show the data in graph using AChartEngine right? Create a new activity and put the chart in that layout. Read here how to use the AChartEngine.

Now when you are passing the data from the first class(activity) to the second class(activity), you can pass the data using the intent. Add extra to the Intent using intent.putExtras().First create a bundle. In that bundle put the data using appropriate method like putSerializableExtra() or putParcelableExtra() and in the second class(activity) call getIntent and catch that intent in a temp variable.

From that you can retrieve the data using intent.getExtras().getSerializableExtra() etc. And you can load the data into AChartEngine data to be displayed as a graph.

Altri suggerimenti

This is a working solution : Starting with WifiActivity activity ,wich will determin wifi scan list ,and send it to the TruitonAChartEngineActivity class.

WifiActivity activity

public class WifiActivity extends Activity  {
       /** Called when the activity is first created. */
    WifiManager wifi;
    Button      enab;
    String resultsString ; 
    String[] myStringArray;
    int aa = 10;
    //tableau pris à partir de http://www.radio-electronics.com/info/wireless/wi-fi/80211-channels-number-frequencies-bandwidth.php
    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},
                                   };



    public class Receiver extends BroadcastReceiver{



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



            if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)){
                final List<ScanResult> results = wifi.getScanResults();

                resultsString = results.toString() ;
                Log.i("resultsString*****************",resultsString);
                final String [ ] [ ] chanelRssi = new String [results.size()][2];



            String[] tabResults = new String[results.size()];
            for(int i=0;i<results.size();i++){  
                if  (results.get(i).frequency /1000 == 2) {
                    for (int j =0;j<14;j++)
                    { if (Center_Frequency_2[1][j] == results.get(i).frequency)
                tabResults[i]=results.get(i).SSID +" (" + results.get(i).BSSID + ") \n"+ results.get(i).frequency +"\n"+ results.get(i).level +"\n"+ results.get(i).capabilities +"\n"+"canal "+Center_Frequency_2[0][j] ;
                    chanelRssi[i][0]=Center_Frequency_2[0][j]+"";
                    chanelRssi[i][1]=results.get(i).level +"";

                    }
                    }
                }
            Button send = (Button) findViewById(R.id.barChartButton);
            send.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("click","click");
                    Toast.makeText(getApplicationContext(),
                            "Position :"+resultsString , Toast.LENGTH_LONG)
                            .show();

                    Intent intent1 = new Intent (getApplicationContext(),TruitonAChartEngineActivity.class); 
                    Bundle bundleObject = new Bundle();
                    bundleObject.putSerializable("key", (Serializable) results);
                    intent1.putExtras(bundleObject);
                    startActivityForResult(intent1,0);


                /*  Intent intent1 = new Intent (getApplicationContext(),TruitonAChartEngineActivity.class);
                    startActivityForResult(intent1,0);*/

                }
            });



             }



        }

        private void startActivities(Intent intent, int i) {
            // TODO Auto-generated method stub

        }


    }


    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi);
        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));

    }



}

And the TruitonAChartEngineActivity class

public class TruitonAChartEngineActivity extends ActionBarActivity {

private static final int SERIES_NR = 2;
String message1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_truiton_achart_engine);
    XYMultipleSeriesRenderer renderer = getTruitonBarRenderer();
    myChartSettings(renderer);
    Bundle v = getIntent().getExtras();
    ArrayList<ScanResult> classObject = (ArrayList<ScanResult>) v.getSerializable("key");
     // message1 = v.getString("message1");
    /* Toast.makeText(getApplicationContext(),
             "Position :" , Toast.LENGTH_LONG)
             .show();*/
    /* Toast.makeText(getApplicationContext(),
             "Position classObject :"+classObject , Toast.LENGTH_LONG)
             .show();*/
     for(int index = 0; index < classObject.size(); index++){

            String  Object = classObject.get(index).level+"";
            Toast.makeText(getApplicationContext(), "Id is :"+Object, Toast.LENGTH_SHORT).show();
        }

     Intent intent = ChartFactory.getBarChartIntent(this, getTruitonBarDataset(), renderer, Type.DEFAULT);
        startActivity(intent);

}}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top