不幸的是,同时检查网络应用是越来越停止(unfortunately app is getting s

2019-07-22 08:11发布

我用下面的代码来检查,通过了我的应用程序的网络连接

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();
    }
  }
}

我已经在manifest文件中定义所需的权限。

每当我试图断开/使用F8键,我会收到“不幸的是APP已经停止” 连接网络,而我没有得到任何打印logcat

我能知道什么是我做错误?

Answer 1:

你有没有在这增加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);
    }
}

注:如果您定位的Androidñ预览。 上述接收机将无法按照由谷歌的约束限制工作。

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



Answer 2:

上面的代码不能正常工作的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. ");
    }
}


Answer 3:

应用定位的Android 7.0(API级别24)和更高,如果他们声明广播接收器在他们的表现不会收到该广播。 如果他们登记其Context.registerReceiver广播接收器()和这方面仍然是有效的应用程序仍然会接收广播。

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



文章来源: unfortunately app is getting stopped while checking for network