hi subodh
try this
package com.FranConnectMobile;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
public class chkInternet extends Activity
{
public boolean isInternetAvailable(Context context){
ConnectivityManager connec = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi =
connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile =
connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(wifi.isConnected() || mobile.isConnected()){
// Check for web site
try{
// Create a URL for the desired page
URL url = new URL("http://www.google.com");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new
InputStreamReader(url.openStream()));
in.close();
return true;
} catch (Exception e) {
return false;
}
}
return false;
}
}
TX
RK
On Wed, Oct 3, 2012 at 6:08 PM, Subodh Nijsure <[email protected]>wrote:
> Hello,
>
> I am trying to implement a service that is supposed to download stuff from
> a cloud service. So I want this service to be notified whenever
> phone/tablet looses network connectivity.
>
>
> So I implemented code that looks like this:
>
> receiver = new ConnectivityReceiver();
> filter = new IntentFilter();
> filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
> try {
> Intent i = registerReceiver(receiver,
> filter);
> }
> catch (Exception e) {
> ....
> }
> However when I have this code in a class that extends a Service
> registerReciver always throws a NullPointerException.
>
> However the same code in a class that extends an activity does not throw
> such an exception. I have attached my manifest file and the full code for
> the service template. Would appreciate any pointers as to why this works in
> activity and not in a service.
>
> Or is there any better way for a service that run in the background to
> know when device looses/gains (IP) network connectivity?
>
> Regards,
> -Subodh
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en