访问wifi的安全例外

时间:2017-10-24 09:35:45

标签: java android

实际上我正试图通过接收器检查后台的网络连接。 编码,一切都好。但显示许可问题。 请尽力帮助我。

以下是我的代码。

NetworkChangeReceiver:

public class NetworkChangeReceiver extends BroadcastReceiver

{

public static final String NETWORK_AVAILABLE_ACTION = "com.onnurinet.NetworkAvailable";
public static final String IS_NETWORK_AVAILABLE = "isNetworkAvailable";

@Override
public void onReceive(Context context, Intent intent)
{
    try
    {
        if (isOnline(context)) {
           // dialog(true,context);
            Toast.makeText(context,"connection is there",Toast.LENGTH_SHORT).show();
            Log.e("TAG", "Online Connect Intenet ");
        } else {
            //dialog(false,context);
            Log.e("TAG", "Conectivity Failure !!! ");
            Toast.makeText(context,"Could not Connect to internet",Toast.LENGTH_SHORT).show();
        }
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}

private boolean isOnline(Context context) {
    try {

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        //should check null because in airplane mode it will be null
        return (netInfo != null && netInfo.isConnected());
    } catch (SecurityException e) {
        e.printStackTrace();
        return false;
    }
}

}

BroadcastEvent类:

public class BroadcastEvent extends Activity {

Boolean networkStatus;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /*int permissionCheck = ContextCompat.checkSelfPermission(BroadcastEvent.this,
            Manifest.permission.ACCESS_NETWORK_STATE);*/
    IntentFilter intentFilter = new IntentFilter(NetworkChangeReceiver.NETWORK_AVAILABLE_ACTION);
    LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            boolean isNetworkAvailable = intent.getBooleanExtra(IS_NETWORK_AVAILABLE, false);
             networkStatus = Boolean.valueOf(isNetworkAvailable ? "connected" : "disconnected");
            if(networkStatus){
                Toast.makeText(getApplicationContext(),"Network Status: " + networkStatus, Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(getApplicationContext(),"Network Status: " + networkStatus, Toast.LENGTH_SHORT).show();
            }
           // Toast.makeText(getApplicationContext(),"Network Status: " + networkStatus, Toast.LENGTH_SHORT).show();
            //Snackbar.make(findViewById(R.id.activity_main), "Network Status: " + networkStatus, Snackbar.LENGTH_LONG).show();
        }
    }, intentFilter);

}

}

清单文件:

<?xml version="1.0" encoding="utf-8"?>

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<supports-screens
    android:anyDensity="true"
    android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:xlargeScreens="true" />

//------------------------------STBService---------------------------------------
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />


<uses-permission android:name="ANDROID.PERMISSION.INTERNET" />
<uses-permission android:name="ANDROID.PERMISSION.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:hardwareAccelerated="false"
    android:icon="@mipmap/airstream_logo"
    android:label="@string/app_name"
    android:name=".MyApplication"
    android:theme="@style/AppTheme">
    <activity
        android:name=".HomeMainActivity"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.AppCompat.NoActionBar" />


    <activity
        android:name=".BroadcastEvent"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="com.onnurinet.receivers.NetworkChangeReceiver">

        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
        </intent-filter>
    </receiver>

请尝试检查我的代码并告诉我哪里做错了。我清理项目并检查谷歌,但我仍然没有得到问题。

错误:

10-24 15:13:16.601 19293-19293/com.onnet.airstream W/System.err: java.lang.SecurityException: ConnectivityService: Neither user 10405 nor current process has android.permission.ACCESS_NETWORK_STATE.

10-24 15:13:16.601 19293-19293 / com.onnet.airstream W / System.err:at android.os.Parcel.readException(Parcel.java:1546) 10-24 15:13:16.601 19293-19293 / com.onnet.airstream W / System.err:at android.os.Parcel.readException(Parcel.java:1499) 10-24 15:13:16.601 19293-19293 / com.onnet.airstream W / System.err:at android.net.IConnectivityManager $ Stub $ Proxy.getActiveNetworkInfo(IConnectivityManager.java:964) 10-24 15:13:16.601 19293-19293 / com.onnet.airstream W / System.err:at android.net.ConnectivityManager.getActiveNetworkInfo(ConnectivityManager.java:610) 10-24 15:13:16.601 19293-19293 / com.onnet.airstream W / System.err:at com.onnurinet.receivers.NetworkChangeReceiver.isOnline(NetworkChangeReceiver.java:41) 10-24 15:13:16.601 19293-19293 / com.onnet.airstream W / System.err:at com.onnurinet.receivers.NetworkChangeReceiver.onReceive(NetworkChangeReceiver.java:23) 10-24 15:13:16.601 19293-19293 / com.onnet.airstream W / System.err:at android.app.ActivityThread.handleReceiver(ActivityThread.java:2642) 10-24 15:13:16.601 19293-19293 / com.onnet.airstream W / System.err:at android.app.ActivityThread.access $ 1800(ActivityThread.java:154) 10-24 15:13:16.601 19293-19293 / com.onnet.airstream W / System.err:at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1398) 10-24 15:13:16.602 19293-19293 / com.onnet.airstream W / System.err:at android.os.Handler.dispatchMessage(Handler.java:102) 10-24 15:13:16.602 19293-19293 / com.onnet.airstream W / System.err:at android.os.Looper.loop(Looper.java:135) 10-24 15:13:16.602 19293-19293 / com.onnet.airstream W / System.err:at android.app.ActivityThread.main(ActivityThread.java:5292) 10-24 15:13:16.602 19293-19293 / com.onnet.airstream W / System.err:at java.lang.reflect.Method.invoke(Native Method) 10-24 15:13:16.602 19293-19293 / com.onnet.airstream W / System.err:at java.lang.reflect.Method.invoke(Method.java:372) 10-24 15:13:16.602 19293-19293 / com.onnet.airstream W / System.err:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904) 10-24 15:13:16.602 19293-19293 / com.onnet.airstream W / System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

3 个答案:

答案 0 :(得分:0)

我认为你需要实现运行时权限

read this

如果设备运行的是Android 5.1或更低版本,或者您的应用的目标SDK为22或更低:如果您在清单中列出了危险权限,则用户必须在安装应用时授予权限;如果他们没有授予权限,系统根本不会安装应用程序。

如果设备运行的是Android 6.0或更高版本,并且您的应用的目标SDK为23或更高:应用必须列出清单中的权限,并且必须在应用运行时请求其所需的每个危险权限。用户可以授予或拒绝每个权限,即使用户拒绝权限请求,应用也可以继续以有限的功能运行。

注意:从Android 6.0(API级别23)开始,用户可以随时撤消任何应用的权限,即使该应用针对较低的API级别也是如此。无论您的应用针对哪种API级别,您都应该测试您的应用,以验证其在缺少所需权限时是否正常运行。

答案 1 :(得分:0)

如果您在Android 6.0或更高版本(API级别&gt; = 23)上运行(支持),则需要实施Requesting Permissions at Run Time

答案 2 :(得分:0)

更改您的代码

<uses-permission android:name="ANDROID.PERMISSION.ACCESS_NETWORK_STATE" />
<uses-permission android:name="ANDROID.PERMISSION.INTERNET" />

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