0

我正在开发一个 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)
4

5 回答 5

3

不要从 Activity 调用该方法。创建一个单独的类并将该方法定义为静态。像下面

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkUtils {

    public static boolean isOnline(Context mContext) {
        ConnectivityManager conMgr = (ConnectivityManager) mContext
                .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;
    }
}

并在 Activity 中将此方法称为

NetworkUtils.isOnline(MainActivity.this);

在你的情况下

Toast.makeText(MainActivity.this,"Online Verification "+NetworkUtils.isOnline(MainActivity.this), Toast.LENGTH_LONG).show();
于 2013-08-23T05:44:28.507 回答
0

在这里你得到 IllegalException 这意味着它在 android 中无效

Runtime(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)

我建议你做这样的事情

 1) BaseActivity extends Activity
 2) OnlineCheck extends BaseActivity
 3) MainActivity extends BaseActivity

然后在这两个活动中使用的代码将其写在 BaseActivity 中。

于 2013-08-23T05:36:37.810 回答
0

2.Activity 先导入MainActivity

例如,您在 2.Activity 上,您将从 2.Activity 调用方法在 MainActivity 方法的名称 callMethode()

活动 1(例如在 MainActivity 上)

public static void callMethode(Context context){
     // Your codes Here
    }

活动二

MainActivity.callMethode(getApplicationContext());
于 2015-02-09T16:39:55.483 回答
0

问题是您正在创建 OnlineCheck 的实例,而没有通过适当的渠道来创建活动。您应该将活动更改为您的在线活动或将 isOnline() 移动到 MainActivity。

请参阅如何在单击按钮时开始新活动以从另一个活动中开始活动。

关键是你不能只调用 new MyActivity()。活动并非旨在以这种方式初始化。

编辑:

如果您真的想要在 OnlineCheck 课程中使用它,这可能会起作用

Toast.makeText(MainActivity.this,"Online Verification "+OnlineCheck.isOnline(this), Toast.LENGTH_LONG).show();

public class OnlineCheck extends Activity
{



    public static boolean isOnline(Activity source) {

        ConnectivityManager conMgr = (ConnectivityManager) source
                .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;
    }

}
于 2013-08-23T05:39:40.300 回答
0

这是你的B级

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;

public class OnlineCheck
{
    public boolean isOnline(Context context) {

        ConnectivityManager conMgr = (ConnectivityManager) context
                .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;
    }

}

并使用以下代码调用

Toast.makeText(MainActivity.this,"Online Verification "+ new OnlineCheck().isOnline(MainActivity.this), Toast.LENGTH_LONG).show();
于 2013-08-23T05:34:10.907 回答