问题
I'm working with Android Studio on an app that, when I push a button, gives me the results of wifi scan! If I test the code alone (only the part "wifi scan") it works... But when I put it in the complete app, the app crashs! Why? I post my code:
//MAIN
package com.example.pc1.tesiprova;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//CERCO GLI ELEMENTI BUTTON E BUTTON2
Button OutDoor = (Button) findViewById(R.id.button);
Button InDoor = (Button) findViewById(R.id.button2);
//CLICCANDO SU BUTTON FACCIO APRIRE UNA NUOVA ACTIVITY
OutDoor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//DEFINISCO L'INTENZIONE DI APRIRE UNA NUOVA ACTIVITY
Intent openActivityO=new Intent(MainActivity.this,ActivityO.class);
//APRO L'ACTIVITY ACTIVITYO.JAVA
startActivity(openActivityO);
}
});
//CLICCANDO SU BUTTON2 FACCIO APRIRE UNA NUOVA ACTIVITY
InDoor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent openActivityI=new Intent(MainActivity.this,ActivityI.class);
startActivity(openActivityI);
}
});
}
//ACTIVITYO (that contains wifi scan!)
package com.example.pc1.tesiprova;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import java.util.List;
public class ActivityO extends AppCompatActivity {
public WifiManager wifi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_o);
TextView text1 = (TextView)findViewById(R.id.textView);
TextView text2 = (TextView)findViewById(R.id.textView2);
TextView text3 = (TextView)findViewById(R.id.textView3);
wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
for(int i=0;i<5;i++);
wifi.startScan();
try{
text1.setText("Sto cercando");
Thread.sleep(3333);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
List<ScanResult> results = wifi.getScanResults();
String ssid1= results.get(0).SSID;
String ssid2= results.get(1).SSID;
String ssid3= results.get(2).SSID;
Integer rssi1= results.get(0).level;
Integer rssi2= results.get(1).level;
Integer rssi3= results.get(2).level;
text1.setText(ssid1+rssi1);
text2.setText(ssid2+rssi2);
text3.setText(ssid3+rssi3);
And the errors are:
10-23 08:30:04.257 2230-2230/com.example.pc1.tesiprova I/art﹕ Not late-enabling -Xcheck:jni (already on)
10-23 08:30:04.257 2230-2230/com.example.pc1.tesiprova I/art﹕ Late-enabling JIT
10-23 08:30:04.259 2230-2230/com.example.pc1.tesiprova I/art﹕ JIT created with code_cache_capacity=2MB compile_threshold=1000
10-23 08:30:04.867 2230-2230/com.example.pc1.tesiprova W/System﹕ ClassLoader referenced unknown path: /data/app/com.example.pc1.tesiprova-1/lib/x86
10-23 08:30:05.276 2230-2252/com.example.pc1.tesiprova D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-23 08:30:05.286 2230-2230/com.example.pc1.tesiprova D/﹕ HostConnection::get() New Host Connection established 0xac07fda0, tid 2230
10-23 08:30:05.459 2230-2252/com.example.pc1.tesiprova D/﹕ HostConnection::get() New Host Connection established 0xac07feb0, tid 2252
10-23 08:30:05.507 2230-2252/com.example.pc1.tesiprova I/OpenGLRenderer﹕ Initialized EGL, version 1.4
10-23 08:30:05.576 2230-2252/com.example.pc1.tesiprova W/EGL_emulation﹕ eglSurfaceAttrib not implemented
10-23 08:30:05.576 2230-2252/com.example.pc1.tesiprova W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xac130fc0, error=EGL_SUCCESS
10-23 08:31:14.500 2230-2252/com.example.pc1.tesiprova W/EGL_emulation﹕ eglSurfaceAttrib not implemented
10-23 08:31:14.500 2230-2252/com.example.pc1.tesiprova W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xac13f560, error=EGL_SUCCESS
10-23 08:31:14.812 2230-2252/com.example.pc1.tesiprova E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xac0763b0
10-23 08:31:14.819 2230-2252/com.example.pc1.tesiprova D/OpenGLRenderer﹕ endAllStagingAnimators on 0xa42e1280 (RippleDrawable) with handle 0xac12fbd0
10-23 08:31:16.517 2230-2252/com.example.pc1.tesiprova W/EGL_emulation﹕ eglSurfaceAttrib not implemented
10-23 08:31:16.517 2230-2252/com.example.pc1.tesiprova W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xad9c7840, error=EGL_SUCCESS
10-23 08:31:17.043 2230-2230/com.example.pc1.tesiprova I/Choreographer﹕ Skipped 30 frames! The application may be doing too much work on its main thread.
10-23 08:31:17.119 2230-2252/com.example.pc1.tesiprova E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xac076c00
10-23 08:31:18.646 2230-2242/com.example.pc1.tesiprova I/art﹕ Background sticky concurrent mark sweep GC freed 9604(599KB) AllocSpace objects, 0(0B) LOS objects, 53% free, 1148KB/2MB, paused 1.649ms total 139.305ms
10-23 08:31:18.671 2230-2242/com.example.pc1.tesiprova W/art﹕ Suspending all threads took: 24.989ms
10-23 08:31:22.070 2230-2230/com.example.pc1.tesiprova D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
10-23 08:31:22.093 2230-2230/com.example.pc1.tesiprova E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.pc1.tesiprova, PID: 2230
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pc1.tesiprova/com.example.pc1.tesiprova.ActivityO}: java.lang.SecurityException: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
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:1066)
at android.net.wifi.WifiManager.getScanResults(WifiManager.java:1311)
at com.example.pc1.tesiprova.ActivityO.onCreate(ActivityO.java:44)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
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)
10-23 08:31:24.168 2230-2230/? I/Process﹕ Sending signal. PID: 2230 SIG: 9
I have used these permissions:
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
Thank you very much!!!!
回答1:
It is possible that you are testing the application on Android 6.0 (for example in emulator). In that case you need to ask for the permission at runtime, it is not sufficient to declare it in manifest.
You need to check if the permission was granted (this is not a copy-paste code for you, you need to adjust it for your needs):
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION))
== PackageManager.PERMISSION_GRANTED) {
//your code that requires permission
}
In order to ask for permission you need to use this code:
ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.ACCESS_COARSE_LOCATION },
REQUEST_CODE);
For more details check developer guide: http://developer.android.com/training/permissions/requesting.html
回答2:
Please add ACCESS_FINE_LOCATION permission in manifest and try again.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
来源:https://stackoverflow.com/questions/33298357/securityexception-when-getting-wifi-scan-results