我试图在弹出窗口中获取wifi扫描结果,但它强制关闭。如果有任何我寻找同样的问题可能提供我的代码,但无法找到结果。 Wifi转身,扫描的烤面包出现后不久就停止了错误 这是我的代码
MainActivity.java
package com.example.hemang.wifi_app;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {
GestureDetectorCompat gd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.gd = new GestureDetectorCompat(MainActivity.this,MainActivity.this);
}
@Override
public boolean onDown(MotionEvent e) {
Intent i = new Intent(MainActivity.this,popup.class);
startActivity(i);
return true;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
this.gd.onTouchEvent(event);
return super.onTouchEvent(event);
}
}
Popup.java
package com.example.hemang.wifi_app;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Toast;
import java.util.List;
public class popup extends Activity {
WifiManager wifiManager;
WifiReceiver wifiReceiver;
RadioButton b1;
Button b2;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popup);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
getWindow().setLayout((int) (width * 0.8), (int) (height * 0.5));
final RadioButton b1 = (RadioButton)findViewById(R.id.radioButton);
Button b2 =(Button)findViewById(R.id.scanbutton);
b1.setOnClickListener(new Button.OnClickListener() {
int counter = 0;
public void onClick(View v) {
if(counter==0){
b1.setChecked(true);
b1.setTextColor(Color.WHITE);
// Toast.makeText(popup.this,"Turning Wifi ON",Toast.LENGTH_SHORT).show();
counter++;
TurnWifion();
}
else
{
b1.setChecked(false);
Toast.makeText(popup.this,"Turning Wifi OFF",Toast.LENGTH_SHORT).show();
counter--;
b1.setTextColor(Color.BLACK);
TurnWifioff();
}
}
});
Button scan =(Button)findViewById(R.id.scanbutton);
b2.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
wifiManager.startScan();
}
});
}
private void TurnWifion() {
WifiReceiver wifiReceiver = new WifiReceiver();
registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifiManager.startScan();
Toast.makeText(popup.this,"Getting Scanned Result",Toast.LENGTH_SHORT).show();
}
private void TurnWifioff() {
}
class WifiReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
List<ScanResult> scanResults = wifiManager.getScanResults();
String[] scan_list = new String[scanResults.size()];
for(int i =0 ;i <= scanResults.size();i++){
scan_list[i]=((scanResults.get(i)).toString());
}
ListView lv = (ListView)findViewById(R.id.listView2);
lv.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_expandable_list_item_1,scan_list));
}
}
}