Android BroadcastReceiver:从onReceive方法获取对象

时间:2014-04-07 13:08:25

标签: android arrays class broadcastreceiver wifi

我正在尝试将包含Wi-Fi信息的数组列表从一个从BroadcastReceiver扩展到另一个类的类传递。我可以使用getter传递两个类之间的正常变量。但我得到一个空数组,因为onReceive方法没有被执行。我想在另一个类中执行onReceive方法(但我不能实例化它)或者在另一个类中获取在onReceive方法中创建的数组使用getter(但我无法得到列表)。请问如何在第二节中获得此数组。

这是WifiData类

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 +"" });
        }

     }

}

}

这是必须获取wifi信息并创建图表的SalesStackedBarChart类:

    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()+"");
     }
} 

谢谢。

2 个答案:

答案 0 :(得分:1)

您是否正在尝试使用AChartEngine在图表中显示数据? 创建一个新活动并将图表放在该布局中。阅读here如何使用AChartEngine。

现在,当您将数据从第一个类(活动)传递到第二个类(活动)时,您可以使用intent传递数据。使用intent.putExtras()向Intent添加额外内容。首先创建一个bundle。在该捆绑包中使用适当的方法(如putSerializableExtra() or putParcelableExtra())放入数据,并在第二个类(活动)中调用getIntent并在临时变量中捕获该意图。

您可以使用intent.getExtras()。getSerializableExtra()等来检索数据。您可以将数据加载到AChartEngine数据中以显示为图形。

答案 1 :(得分:0)

这是一个有效的解决方案: 从WifiActivity活动开始,将确定wifi扫描列表,并将其发送到TruitonAChartEngineActivity类。

WifiActivity活动

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

    }



}

和TruitonAChartEngineActivity类

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

}}