@Alan Moore: Hi, I think I finally did it! This is the code that was able to perform a Wi-Fi check and send a response back to the calling activity using the Broadcast intent.
LargeImageScoll.java (activity)
Intent intent; public static Runnable scanTask; public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //updateUI(intent); //String returnedValue = intent.getStringExtra("data"); Bundle bundle = intent.getExtras(); String returnedValue = bundle.getString("data"); Log.e("Waht", returnedValue); } }; private static Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); } }; public class LargeImageScroller extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new SampleView(this)); intent = new Intent(this, WifiScanning.class); } @Override public boolean onCreateOptionsMenu(Menu menu) {...} @Override public boolean onOptionsItemSelected(MenuItem item) {...} //this is the class where the program would do all the UI and display images private static class SampleView extends View { public SampleView(Context context) { : loadMap load = new loadMap(); load.execute(context); scanTask = new Runnable(){ @Override public void run() { // TODO Auto-generated method stub startService(new Intent(context, WifiScanning.class)); }; handler.removeCallbacks(scanTask); handler.postDelayed(scanTask, refreshRate); } public boolean onTouchEvent(MotionEvent event) {...} protected void onDraw(Canvas canvas) {...} private static Drawable LoadImageFromWebOperations(String url){...} private static Bitmap decodeFile(File f, int requiredSize){...} private class loadMap extends AsyncTask<Context, Void, ArrayList<Bitmap>>{...} }//end of SampleView protected void onResume() { // TODO Auto-generated method stub Log.e("AndroidRuntime", "onResume"); handler.removeCallbacks(scanTask); handler.postDelayed(scanTask, refreshRate); registerReceiver(broadcastReceiver, new IntentFilter(WifiScanning.BROADCAST_ACTION)); super.onResume(); } @Override protected void onStop() { // TODO Auto-generated method stub Log.e("AndroidRuntime", "onStop"); unregisterReceiver(broadcastReceiver); stopService(new Intent(this, WifiScanning.class)); handler.removeCallbacks(scanTask); super.onStop(); } }//end of LargeImageScroll
WifiScanning.java (service)
public static final String BROADCAST_ACTION = "android.wps.wifiscanning.broadcasttest"; int counter = 0; Intent intent1; WifiReceiver receiverWifi = new WifiReceiver(); WifiManager wifi; StringBuilder sb; List<ScanResult> wifiList; public void onCreate() { super.onCreate(); intent1 = new Intent(BROADCAST_ACTION); Log.e(TAG, "Service creating"); wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); if(counter==0){ Log.e("AndroidRuntime", "Scan for the "+counter+" time"); wifi.startScan(); counter++; } } class WifiReceiver extends BroadcastReceiver { public void onReceive(Context c, Intent intent) { sb = new StringBuilder(); wifiList = wifi.getScanResults(); for(int i = 0; i < wifiList.size(); i++){ sb.append("["+ (wifiList.get(i).SSID).toString() + "]["); sb.append((wifiList.get(i).BSSID).toString() + "]["); sb.append((String.valueOf(wifiList.get(i).level)) + "]"); sb.append("\n"); } Log.e("AndroidRuntime", sb.toString()); if(counter<4){ Log.e("AndroidRuntime", "Scan for the "+counter+" time"); wifi.startScan(); counter++; }else{ intent1 = new Intent(BROADCAST_ACTION); String test = sb.toString(); intent1.putExtra("data", test); sendBroadcast(intent1); } } } @Override public void onDestroy() {
user918197
source share