在检查网络时,APP被停止了。

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (17)

我正在使用以下代码来检查整个应用程序的互联网连接。

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

public class ConnectionChangeReceiver extends BroadcastReceiver
{
  @Override
  public void onReceive( Context context, Intent intent )
  {
    ConnectivityManager connectivityManager = (ConnectivityManager) 
                       context.getSystemService( Context.CONNECTIVITY_SERVICE );
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo
                                             (ConnectivityManager.TYPE_MOBILE );
    if ( activeNetInfo != null )
    {
      Toast.makeText( context, "Active Network Type : " + 
                       activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
    }
    if( mobNetInfo != null )
    {
      Toast.makeText( context, "Mobile Network Type : " + 
                          mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
    }
  }
}

并且我在清单文件中定义了所需的权限。

每当我试图用F8键断开/连接网络时,我就会收到“应用程序停止了”,而且我不会在logcat中得到任何输出。错误是什么?

提问于
用户回答回答于

你把这个加进去了吗?androidmanifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

如果你已经添加了上面的代码,那么请检查以下代码:

<receiver android:name=".UpdateReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

public class UpdateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {


          ConnectivityManager connectivityManager = (ConnectivityManager) 
                                       context.getSystemService(Context.CONNECTIVITY_SERVICE );
          NetworkInfo activeNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
          boolean isConnected = activeNetInfo != null && activeNetInfo.isConnectedOrConnecting();   
          if (isConnected)       
              Log.i("NET", "connecte" +isConnected);   
          else Log.i("NET", "not connecte" +isConnected);
        }
    }
用户回答回答于

上述代码不适用于Wi-Fi连接。这是修改的

简单的一个

。AndroidManifest.xml中

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

androidmanifest.xml的应用程序标签内

<receiver android:name="android.YOUR-JAVA-CLASS-PATH.BackgroundSync" android:enabled="true">
      <intent-filter>
          <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
      </intent-filter>
</receiver>

BackgroundSync.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager; 
import android.util.Log;
import android.widget.Toast;

public class BackgroundSync extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) { 
        ConnectivityManager cm =(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm.getActiveNetworkInfo()!=null){
            Toast.makeText(context, "Connected to Internet", Toast.LENGTH_LONG).show();
        }
        else Log.i("INTERNET","---------------------> Internet Disconnected. ");
    }
}

扫码关注云+社区