我正在尝试在Android设备上运行我的应用程序,我遇到以下问题:
FATAL EXCEPTION: main
Process: joserafael.clienteandroid, PID: 13790
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS flg=0x4000010 (has extras) } in joserafael.clienteandroid.MainActivity$WifiReceiver@a3c37b3
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:892)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.SecurityException: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at android.net.wifi.IWifiManager$Stub$Proxy.getScanResults(IWifiManager.java:1074)
at android.net.wifi.WifiManager.getScanResults(WifiManager.java:1318)
at joserafael.clienteandroid.MainActivity$WifiReceiver.onReceive(MainActivity.java:173)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:882)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
这是我的活动:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
WifiManager mainWifi;
WifiReceiver receiverWifi;
private final Handler handler = new Handler();
ArrayList<WifiInfo> wifis = new ArrayList<WifiInfo>();
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
receiverWifi = new WifiReceiver();
registerReceiver(receiverWifi, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
if(mainWifi.isWifiEnabled()==false)
{
mainWifi.setWifiEnabled(true);
}
WifiManager.MulticastLock multicastLock = mainWifi.createMulticastLock("mydebuginfo");
multicastLock.acquire();
doInback();
textView = (TextView) findViewById(R.id.textView);
Button btWifi = (Button) findViewById(R.id.btWifi);
btWifi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveWifiData();
}
});
UDP_Server Server = new UDP_Server();
Server.receive();
setTextView("Access : " + Server.Message);
}
public void doInback()
{
handler.postDelayed(new Runnable() {
@Override
public void run()
{
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
receiverWifi = new WifiReceiver();
registerReceiver(receiverWifi, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mainWifi.startScan();
doInback();
}
}, 1000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Refresh");
return super.onCreateOptionsMenu(menu);}
@Override
public void onClick(View v) {
final ActionBar bar = getSupportActionBar();
int flags = 0;
switch (v.getId()) {
}
}
@Override
protected void onPause()
{
unregisterReceiver(receiverWifi);
super.onPause();
}
@Override
protected void onResume()
{
registerReceiver(receiverWifi, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
@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.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void setTextView(String msg){
System.out.println(msg);
textView.setText(msg);
}
public void saveWifiData() {
UDP_Client Client = new UDP_Client();
//Set message
for(WifiInfo info : wifis){
String data = info.toPipe();
Client.Message += data;
}
Client.sender();
}
class WifiReceiver extends BroadcastReceiver
{
public void onReceive(Context c, Intent intent)
{
wifis.clear();
List<ScanResult> wifiList;
wifiList = mainWifi.getScanResults();
for(int i = 0; i < wifiList.size(); i++)
{
ScanResult wifi = wifiList.get(i);
WifiInfo info = new WifiInfo();
info.setBssid(wifi.BSSID);
info.setSignal(wifi.level);
info.setSsid(wifi.SSID);
info.setFrequency(wifi.frequency);
wifis.add(info);
}
// setTextView("Access points found: " + wifis.size());
}
}
}
我已经添加到了清单:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
你能帮助我吗?