Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6732145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:36:52+00:00 2026-05-26T10:36:52+00:00

First of all I’m a novice to android, so this could probably be a

  • 0

First of all I’m a novice to android, so this could probably be a silly issue, nevertheless I’ve already spent a couple of days trying to get to a solution.

I’m trying to build a wifi module for localization purpouses, so I wrote a BroadcastReceiver in order to handle the wifi scanning and the localization. The application works and does its (quite simple at this stage) job, with anu kind of issues both when I change the orientation of the screen and when I hit the back button on my Desire HD and then open the application again. But when I hit the HOME key, going to the main screen, and then enter again my app the Broadcast Receiver seems not to work anymore, and if I close the application I get an error message.

Here’s the code, partially adapted from here.

public class WiFiDemo extends Activity implements OnClickListener {

private static final String TAG = "WiFiDemo";
WifiManager wifi;
BroadcastReceiver receiver;
WifiManager.WifiLock lock;
boolean wifiPrevState;
boolean scanON = false;
String header;


TextView textStatus;
Button buttonScan;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    // Setup UI
    textStatus = (TextView) findViewById(R.id.textStatus);
    buttonScan = (Button) findViewById(R.id.buttonScan);
    buttonScan.setOnClickListener(this);

    // Setup WiFi
    if (wifi == null){
        wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    }


    //checking WiFi status, enabling it if needed and locking it.
    wifiPrevState = wifi.isWifiEnabled();
    wifi.setWifiEnabled(true);
    if (lock == null){
        lock = wifi.createWifiLock("lock");
    }

    lock.acquire();

    // Get WiFi status
    WifiInfo info = wifi.getConnectionInfo();
    header="\n\nWiFi Status: \n" + info.toString() + "\n\nAvailable nets:";
    textStatus.append(header);

    // Register Broadcast Receiver
    if (receiver == null)
        receiver = new WiFiScanReceiver(this);

    registerReceiver(receiver, new IntentFilter(
            WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    Log.d(TAG, "onCreate()");

}

/*
@Override
protected void onPause(){
    super.onPause();
    wifi.setWifiEnabled(wifiPrevState);
    lock.release();
    unregisterReceiver(receiver);
    Log.d(TAG, "onPause()");
}

@Override
protected void onResume(){
    super.onResume();
    wifi.setWifiEnabled(true);
    lock.acquire();
    registerReceiver(receiver, new IntentFilter(
            WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    Log.d(TAG, "onResume()");

}
*/

@Override
public void onStop() {
    super.onStop();
    wifi.setWifiEnabled(wifiPrevState);
    lock.release();
    unregisterReceiver(receiver);

}

public void onClick(View view) {
    Toast.makeText(this, "On Click Clicked. Toast to that!!!",
            Toast.LENGTH_LONG).show();

    if (view.getId() == R.id.buttonScan) {
        Log.d(TAG, "onClick() wifi.startScan()");
        scanON = !scanON;
        wifi.startScan();
    }
}

}

And this is the BroadcastReceiver

public class WiFiScanReceiver extends BroadcastReceiver {
  private static final String TAG = "WiFiScanReceiver";
  WiFiDemo wifiDemo;
  ScanResult storedBest;

  public WiFiScanReceiver(WiFiDemo wifiDemo) {
    super();
    this.wifiDemo = wifiDemo;
    storedBest = null;
  }

 @Override
 public void onReceive(Context c, Intent intent) {
    List<ScanResult> results = wifiDemo.wifi.getScanResults();
    ScanResult bestSignal = null;
    wifiDemo.textStatus.setText(wifiDemo.header);

    for (ScanResult result : results) {
      if (bestSignal == null
         || WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0)
         bestSignal = result;
      wifiDemo.textStatus.append("\n\n" + result.toString());
    }

      if ( storedBest == null || ((bestSignal.SSID.compareTo(storedBest.SSID)!=0) &&  bestSignal.level>-50)){
         storedBest = bestSignal;
         String locationMessage = String.format("You are near %s's Access Point",
                 bestSignal.SSID);
         Toast.makeText(wifiDemo, locationMessage, Toast.LENGTH_LONG).show();
      }
      String message = String.format("%s networks found. %s is the strongest. Its level is %s",
         results.size(), bestSignal.SSID, bestSignal.level);
      if (wifiDemo.scanON) wifiDemo.wifi.startScan();
      Log.d(TAG, "onReceive() message: " + message);
  }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:36:52+00:00Added an answer on May 26, 2026 at 10:36 am

    When posting, its best to post the error message that you are getting so we know the problem you are having.

    That being said, the reason it probably isn’t working is because you unregister your receiver in onStop and you only register your receiver in onCreate. You should typically do these type of calls in life cycle stages that match.

    • onCreate/onDestroy
    • onStart/onStop
    • onResume/onPause.

    To fix your problem, try registering your receiver in onStart instead of onCreate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all there is probably a question like this already but i couldn't
First of all I would like apology if this question has already been answered,
First of all, this isn't for a keylogger, it's for an input in a
First of all, I'm quite new to the Android and JAVA world (coming from
first of all i would like to say i know its probably an easy
First of all, apologize because I have seen some posts about this, but I
First of all, I've googled for this error and all of the pages I
First of all, I am sorry if this question doesn't belong to SO since
first of all, I'd like to explain why i need this. i need to
first of all sorry for the title. I know this is not so clear

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.