I would like to make a program to obtain the radio wave intensity of the access point live.

Asked 2 years ago, Updated 2 years ago, 34 views

Currently, we are developing an application to obtain AP radio intensity through Android, but we can only obtain AP radio intensity when executed.What should I do to obtain the intensity of the radio wave at all times during startup?
Thank you for your cooperation.

public class WifiManager02 extensions ListActivity{
/** Called when the activity is first created.*/
@ Override
public void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    WifiManager=(WifiManager) getSystemService(WIFI_SERVICE);
    if(manager.getWifiState()==WifiManager.WIFI_STATE_ENABLED){
        // Scan APs
        manager.startScan();
        // Get scan results
        List<ScanResult>apList=manager.getScanResults();
        String [ ] aps = new String [apList.size()];
        for(inti=0;i<apList.size();i++){
            aps[i] = "SSID:" + apList.get(i).SSID+"\n"
            + apList.get(i).frequency+"MHz"+apList.get(i).level+"dBm";
            }
        ArrayAdapter<String>adapter=newArrayAdapter<String>(this,android.R.layout.simple_list_item_1,aps);
        setListAdapter (adapter);
        }
    }

@ Override
public boolean onCreateOptionsMenu(android.view.Menu){
    // Inflate the menu; this add items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_wifi_manager02,menu);
    return true;
}

@ Override
public boolean onOptionsItemSelected(android.view.MenuItem){
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, solo
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    // no inspection SimplifiableIfStatement
    if(id==R.id.action_settings){
        return true;
    }

    return super.onOptionsItemSelected(item);
}

android java

2022-09-29 21:39

1 Answers

Always

during bootup

So let's say we exclude background resident apps that use Service instead of Activity.

There are many ways to implement it, but I think I will use Thread.

Since onCreate runs at a certain time during the app startup process, it runs once in one activity lifecycle (and of course, it runs again when it is restarted).

For example, the Executors class is a Java class for generating and running Thread periodically.This allows Thread to run at regular intervals.The following are examples of running at intervals of 10 seconds:

scheduledExecutorService=Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.scheduleAtFixedRate(newRunnable(){
    @ Override
    public void run() {
        getActivity().runOnUiThread(newRunnable(){
            @ Override
            public void run() {
                // Describe a set of actions to scan Wi-Fi
                // (Maybe it will be more readable if you call it in a method.)
                WifiManager=(WifiManager) getSystemService(WIFI_SERVICE);
                if(manager.getWifiState()==WifiManager.WIFI_STATE_ENABLED){
                    // Scan APs
                    ...
                }
            }
        });
    }
}, 0,10, TimeUnit.SECONDS);

Thread itself is a different thread from UiThread.On the other hand, most of the code presented requires UiThread processing.Therefore, among the Threads generated by Executors,
The key point is to run the Activity.runOnUiThread method to run UiThread.That's why the Runnable object is duplicated.

In addition to Executors, there are other implementations that use the Timer class, but Thread is the same thing.


2022-09-29 21:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.