Reputation: 51
I am programming a service that runs when you boot the phone, but you need an Internet connection and connection to send SMS operator.
I wrote a function to ConnectionManager
as in other post appears, but I always get the same answer: NullPointerException
, when I do the following:
I define cm
as ConnectionManager
and
NetworkInfo NetInfo = cm.getNetworkInfo();
Is it not because of this?
How can I fix it?
Sorry if I did not put all the code ...
I have the following in the OnStart () method of a service that is activated by ACTION_BOOT_COMPLETED ...
@ Override
public void OnStart (Intent intent, int start) {
Context aplCtx = getApplicationContext ();
IsConnected boolean = false;
while (! IsConnected) {
try {
IsConnected = IsOnline (aplCtx);
} Catch (Exception e) {
Log.d (e.toString ());
}
}
.....
}
private boolean IsOnline (Context ctx) {
boolean result = false;
try {
ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService (Context.CONNECTIVITY_SERVICE);
cm.getActiveNetworkInfo NetworkInfo ni = ();
if (ni! = null) {
if (ni.getState () == NetworkInfo.State.CONNECTED) {
result = true;
}
}
return result;
}
Since this is a background function and after starting the Smartphone, the operating system to force kill the process, before we get an Internet connection. How I can resolve this?, How do I get the operating system does not kill the process and have an Internet connection to perform the following actions?
Upvotes: 2
Views: 1849
Reputation: 109237
Without your code, We can't say anything what going wrong,
But to get network info I am using this, If you are not make a mistake then it also works,
public static boolean netConnect(Context ctx){
ConnectivityManager cm ;
NetworkInfo info = null;
try {
cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
info = cm.getActiveNetworkInfo();
} catch (Exception e) {
Log.e("connectivity", e.toString());
}
if(info != null){
return info.isConnected();
}else{
return false;
}
}
Upvotes: 1