19

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

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

我能知道我在做什么错吗?

4

3 回答 3

65

注意:如果您的目标是 android N Preview。上述接收器将无法按照 Google 的限制工作。

链接:https ://developer.android.com/preview/features/background-optimization.html#connectivity-action

使用:同样的 WorkManager 或 JobScheduler


你把这个加进去了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", "Connected" + isConnected);   
          else 
              Log.i("NET", "Not Connected" + isConnected);
    }
}

于 2013-03-21T11:58:01.197 回答
6

上面的代码不适用于 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. ");
    }
}
于 2016-05-06T07:07:37.660 回答
1

面向 Android 7.0(API 级别 24)及更高版本的应用如果在其清单中声明广播接收器,则不会收到此广播。如果应用程序使用 Context.registerReceiver() 注册其 BroadcastReceiver 并且该上下文仍然有效,它们仍将接收广播。

https://developer.android.google.cn/reference/android/net/ConnectivityManager.html?hl=zh-cn#CONNECTIVITY_ACTION

于 2017-07-05T04:04:56.567 回答