70721

ConnectivityManager is crashing while getting network info

I am new to android and when i give the following snippet, my android application is crashing.

ConnectivityManager manager= (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE); manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); // Application is crashing in this line.

I also checked the androidmanifest.xml where i gave the internet permission to the application. Any help would be really useful for my further work.

-Thanks, Ron..

Answer1:

I am not sure what you want to say but if you want to check internet connection then you can use this code

/** * THIS IS FUNCTION FOR CHECKING INTERNET CONNECTION * @return TRUE IF INTERNET IS PRESENT ELSE RETURN FALSE */ public boolean checkInternetConnection() { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo(); return (activeNetworkInfo != null && activeNetworkInfo.isAvailable() && activeNetworkInfo.isConnected()); }

and dont forget

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

Answer2:

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

Recommend

  • Trying to find and install libstdc++.so.5 to run C++ with mex for MATLAB
  • How to set a URL scheme/link for a react-native app
  • How to convert list into string with quotes in python
  • PhoneGap : How to upload APK files on Google Play Store
  • How to remove left and right margins from all wrapped rows in flexbox (without nth-child or js)
  • Android NFC read Tags issue. Activity starts each time on data received
  • JSON data through JS/AJAX into PHP
  • Phonegap GA plugin not working
  • calculating number of bytes of each row in an image
  • Application bar icon text length
  • Retrieve IP address of device
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • what makes a request a new request in asp.net C#
  • How to make R's read_csv2() recognise the text characters properly
  • cygwin cannot exec 'git-add--interactive' permission denied
  • Check for zero lines output from command over SSH
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • Spring Cloud Microservice Architecture Confusion
  • Marklogic : Query response time is very high
  • How to set ini file attributes during an Inno install
  • Play WS (2.2.1): post/put large request
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • Using $this when not in object context
  • Deselecting radio buttons while keeping the View Model in synch
  • Nant, Vault & Windows Integrated Authentication
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Perl system calls when running as another user using sudo
  • Deserializing XML into class C#
  • Display Images one by one with next and previous functionality
  • Is there a mandatory requirement to switch app.yaml?
  • Use group_by to filter specific cases while keeping NAs
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • How to Embed XSL into XML
  • java string with new operator and a literal