获取Wifi扫描结果时出现SecurityException

时间:2015-10-23 08:51:43

标签: android crash wifi android-wifi wifimanager

我正在使用Android Studio上的应用程序,当我按下按钮时,会给我wifi扫描的结果! 如果我单独测试代码(只有部分“wifi扫描”)它可以工作......但是当我把它放在完整的应用程序中时,应用程序崩溃了! 为什么? 我发布了我的代码:

// 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(包含wifi扫描!)

    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);

错误是:

    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

我使用了这些权限:

<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" />

非常感谢!!!!

2 个答案:

答案 0 :(得分:5)

您可能正在Android 6.0上测试应用程序(例如在模拟器中)。在这种情况下,您需要在运行时请求权限,仅在清单中声明它是不够的。

您需要检查是否已授予权限(这不是复制粘贴代码,您需要根据需要进行调整):

if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION))
                     == PackageManager.PERMISSION_GRANTED) {
    //your code that requires permission
}

要获得许可,您需要使用此代码:

ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.ACCESS_COARSE_LOCATION },
                REQUEST_CODE);

有关详细信息,请查看开发人员指南:http://developer.android.com/training/permissions/requesting.html

答案 1 :(得分:1)

请在清单中添加 ACCESS_FINE_LOCATION 权限,然后重试。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>