How to get wifiList using getter from Standalone class extends BroadcastReceiver?

喜夏-厌秋 提交于 2019-12-12 00:34:57

问题


I create a project using Wifi. I implement a standalone class(WifiScanReceiver) extends BroadcastReceiver. I try to call the standalone class from MainActivity. I want to trigger the wifiList alertdialog when I touch a ImageView. Now I got the null pointer exception.

In here It shown another one error You need to use a Theme.AppCompat theme (or descendant) with this activity.

Please help me to solve these problems.Any help I would really appreciated.

MainActivity:

public class MainActivity extends AppCompatActivity {

private boolean isWifiStart = false;

WifiManager wifiManager;
String[] strWifiList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    wifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);


    ImageView imgView = (ImageView)findViewById(R.id.floorPlan);
    imgView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (isWifiStart) {
                LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View inflateView = inflater.inflate(R.layout.dialog_wifi_list, null);
                AlertDialog.Builder builder = new AlertDialog.Builder(getBaseContext());

                WifiScanReceiver receiver = new WifiScanReceiver(wifiManager);
                registerReceiver(receiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
                wifiManager.startScan();

                try {
                    strWifiList = receiver.getWifiListString();
                } catch (Exception e) {
                    shortMsg(getBaseContext(),"Exception : " + e.getMessage());
                }

                ListView wifiList = (ListView)inflateView.findViewById(R.id.list_Wifi);
                builder.setView(inflateView);
                wifiList.setAdapter(new ArrayAdapter<>(getBaseContext(), android.R.layout.simple_list_item_1,strWifiList));
                builder.setView(inflateView);
                builder.show();
            }
            return true;
        }
    });
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch (item.getItemId()) {
        case R.id.wifiStart:
            shortMsg(this,"Wifi Start");
            isWifiStart = true;
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }

}

public void shortMsg(Context ctx,String msg) {
    Toast.makeText(ctx,msg,Toast.LENGTH_SHORT).show();
}
}

WifiScanReceiver :(Standalone class)

public class WifiScanReceiver extends BroadcastReceiver {

private String[] wifiListString;
private WifiManager wifiMgr;

public WifiScanReceiver(WifiManager manager) {
    this.wifiMgr = manager;
}

@Override
public void onReceive(Context context, Intent intent) {
    List<ScanResult> wifiScanList = wifiMgr.getScanResults();

    Collections.sort(wifiScanList, new Comparator<ScanResult>() {
        @Override
        public int compare(ScanResult lhs, ScanResult rhs) {
            return (lhs.level > rhs.level ? -1 : (lhs.level == rhs.level ? 0 : 1));
        }
    });

    wifiListString = new String[wifiScanList.size()];

    for (int i = 0; i < wifiScanList.size(); i++) {
        wifiListString[i] = (wifiScanList.get(i).SSID);
    }
}

public String[] getWifiListString() {
    return wifiListString;
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.rewifiheatmap" >

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

来源:https://stackoverflow.com/questions/32437690/how-to-get-wifilist-using-getter-from-standalone-class-extends-broadcastreceiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!