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:
页: 1
NetworkInfo NetInfo = cm.getNetworkInfo();
不是因为这种情况?
我怎么能够确定这一点?
如果我没有制定所有法典,那么我会问:
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?