我正在开发一个 Android 应用程序,我必须在其中验证设备是处于在线模式还是离线模式(即,我必须验证连接服务是否可用)。
我有两个班级 class A 和 Class B 。
在 A 类中,我尝试了如下代码片段,
package com.example.onlineverification;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//new OnlineCheck().onCreate(savedInstanceState)
Toast.makeText(MainActivity.this,"Online Verification "+new OnlineCheck().isOnline(), Toast.LENGTH_LONG).show();
}
});
}
}
在 B 类中,我尝试过的代码片段如下所示,
package com.example.onlineverification;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
public class OnlineCheck extends Activity
{
public boolean isOnline() {
ConnectivityManager conMgr = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] networkInfo = conMgr.getAllNetworkInfo();
boolean state=false;
for(NetworkInfo nInfo:networkInfo){
if(nInfo.getType()==ConnectivityManager.TYPE_WIFI || nInfo.getType()==ConnectivityManager.TYPE_ETHERNET || nInfo.getType()==ConnectivityManager.TYPE_MOBILE){
if (nInfo.getState() == NetworkInfo.State.CONNECTED
|| nInfo.getState() == NetworkInfo.State.CONNECTING) {
state=true;
break;
}
}
}
return state;
}
}
当我单击按钮时,应用程序崩溃并且我的 Logcat 错误如下所示,
08-23 10:55:29.152: E/AndroidRuntime(25945): FATAL EXCEPTION: main
08-23 10:55:29.152: E/AndroidRuntime(25945): java.lang.IllegalStateException: System services not available to Activities before onCreate()
08-23 10:55:29.152: E/AndroidRuntime(25945): at android.app.Activity.getSystemService(Activity.java:4463)
08-23 10:55:29.152: E/AndroidRuntime(25945): at com.example.onlineverification.OnlineCheck.isOnline(OnlineCheck.java:18)
08-23 10:55:29.152: E/AndroidRuntime(25945): at com.example.onlineverification.MainActivity$1.onClick(MainActivity.java:24)
08-23 10:55:29.152: E/AndroidRuntime(25945): at android.view.View.performClick(View.java:4204)
08-23 10:55:29.152: E/AndroidRuntime(25945): at android.view.View$PerformClick.run(View.java:17355)
08-23 10:55:29.152: E/AndroidRuntime(25945): at android.os.Handler.handleCallback(Handler.java:725)
08-23 10:55:29.152: E/AndroidRuntime(25945): at android.os.Handler.dispatchMessage(Handler.java:92)
08-23 10:55:29.152: E/AndroidRuntime(25945): at android.os.Looper.loop(Looper.java:137)
08-23 10:55:29.152: E/AndroidRuntime(25945): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-23 10:55:29.152: E/AndroidRuntime(25945): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 10:55:29.152: E/AndroidRuntime(25945): at java.lang.reflect.Method.invoke(Method.java:511)
08-23 10:55:29.152: E/AndroidRuntime(25945): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-23 10:55:29.152: E/AndroidRuntime(25945): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-23 10:55:29.152: E/AndroidRuntime(25945): at dalvik.system.NativeStart.main(Native Method)