Reputation: 1312
I'm trying to make a small function to see if the android running my app has Online Connection.
I already tried to run it, but I'm getting an error on this line:
NetworkInfo [] info = connectivity.getAllNetworkInfo();
Here's my code (button just to make a small message saying if I'm connected to the internet or not)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button check = (Button) findViewById(R.id.buttonCheck);
check.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ConnectionDetector Connection = new ConnectionDetector(getApplicationContext());
boolean hasConnection = Connection.isConnectionToInternet();
if(hasConnection) {
Toast.makeText(getApplicationContext(), "Está ligado à internet", Toast.LENGTH_LONG);
}else {
Toast.makeText(getApplicationContext(), "Não está ligado à internet", Toast.LENGTH_LONG);
}
}
});
ConnectionDetector Class
package com.example.testlayout.app;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionDetector {
private Context context ;
public ConnectionDetector(Context context){
this.context = context ;
}
public boolean isConnectionToInternet(){
ConnectivityManager connectivity = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivity != null )
{
NetworkInfo [] info = connectivity.getAllNetworkInfo();
if(info != null)
for(int i = 0 ; i < info.length ;i++)
if(info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true ;
}
}
return false ;
}
}
Logcat:
04-10 16:15:35.597 26543-26543/com.example.testlayout.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.testlayout.app, PID: 26543
java.lang.SecurityException: ConnectivityService: Neither user 10117 nor current process has android.permission.ACCESS_NETWORK_STATE.
at android.os.Parcel.readException(Parcel.java:1465)
at android.os.Parcel.readException(Parcel.java:1419)
at android.net.IConnectivityManager$Stub$Proxy.getAllNetworkInfo(IConnectivityManager.java:881)
at android.net.ConnectivityManager.getAllNetworkInfo(ConnectivityManager.java:620)
at com.example.testlayout.app.ConnectionDetector.isConnectionToInternet(ConnectionDetector.java:22)
at com.example.testlayout.app.MainActivity$1.onClick(MainActivity.java:56)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 813
Reputation: 9700
Since you didn't add uses-permission
to access network state that's why its throwing the Exception
...
java.lang.SecurityException: ConnectivityService: Neither user 10117 nor current process has android.permission.ACCESS_NETWORK_STATE.
Now, add this permission in your Manifest
file...
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Also add following permission if you want to know wifi network state.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Upvotes: 0
Reputation: 4185
Add this to your Manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Upvotes: 1