监听网络变化的程序,Toast功能出不来,报错也不知道怎么拯救这个错误 5C

package com.example.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.NetworkOnMainThreadException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private IntentFilter intentFilter;

private NetworkChangeReceiver networkChangeReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    intentFilter = new IntentFilter();
    intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
    networkChangeReceiver = new NetworkChangeReceiver();
    registerReceiver(networkChangeReceiver, intentFilter);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(networkChangeReceiver);
}

class NetworkChangeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isAvailable()) {
            Toast.makeText(context, "network is available", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(context, "network is unavailable", Toast.LENGTH_SHORT).show();
        }
    }
}

}

AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
package="com.example.broadcasttest">

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

报错
02-26 02:59:02.161 1590-1590/? I/art: Late-enabling -Xcheck:jni
02-26 02:59:02.162 1590-1590/? W/art: Unexpected CPU variant for X86 using defaults: x86
02-26 02:59:02.705 1590-1590/com.example.broadcasttest W/System: ClassLoader referenced unknown path: /data/app/com.example.broadcasttest-1/lib/x86
02-26 02:59:02.983 1590-1590/com.example.broadcasttest W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable

                                                          [ 02-26 02:59:03.293  1590: 1590 D/         ]
                                                          HostConnection::get() New Host Connection established 0xe5cf49c0, tid 1590


                                                          [ 02-26 02:59:03.451  1590: 1590 W/         ]
                                                          Process pipe failed

02-26 02:59:03.573 1590-1605/com.example.broadcasttest D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
02-26 02:59:03.575 1590-1605/com.example.broadcasttest E/libEGL: load_driver(/system/lib/egl/libGLES_emulation.so): dlopen failed: library "/system/lib/egl/libGLES_emulation.so" not found
02-26 02:59:03.579 1590-1605/com.example.broadcasttest D/libEGL: loaded /system/lib/egl/libEGL_emulation.so
02-26 02:59:03.582 1590-1605/com.example.broadcasttest D/libEGL: loaded /system/lib/egl/libGLESv1_CM_emulation.so
02-26 02:59:03.613 1590-1605/com.example.broadcasttest D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so
02-26 02:59:03.818 1590-1605/com.example.broadcasttest I/OpenGLRenderer: Initialized EGL, version 1.4
02-26 02:59:03.818 1590-1605/com.example.broadcasttest D/OpenGLRenderer: Swap behavior 1
02-26 02:59:04.294 1590-1605/com.example.broadcasttest E/EGL_emulation: tid 1605: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
02-26 02:59:04.294 1590-1605/com.example.broadcasttest W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xcc924180, error=EGL_BAD_MATCH
02-26 02:59:04.309 1590-1590/com.example.broadcasttest I/Choreographer: Skipped 32 frames! The application may be doing too much work on its main thread.
02-26 02:59:05.625 1590-1605/com.example.broadcasttest E/EGL_emulation: tid 1605: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
02-26 02:59:05.625 1590-1605/com.example.broadcasttest W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xcc924400, error=EGL_BAD_MATCH

3个回答

dlopen failed: library "/system/lib/egl/libGLES_emulation.so" not found。。。。。。。。。应该是这个错误

weixin_38335734
-Tiger- 这个错误不知道怎么解决
2 年多之前 回复

sendBroadcast(new Intent("android.net.conn.CONNECTIVITY_CHANGE"))你少写了这一句,没认真看书

YXTS122
YXTS122 不好意思,我回答错了,昨天我运行了你的代码,很正常,没报错啊,toast也能出现,会不会是你的手机或者模拟机有问题啊
2 年多之前 回复
YXTS122
YXTS122 不好意思,我回答错了,昨天我运行了你的代码,很正常,没报错啊,toast也能出现,会不会是你的手机或者模拟机有问题啊
2 年多之前 回复

你的手机 或者模拟器 系统的版本 是4.几的吗 如果是 我怀疑 4.几系统 不支持在广播中Toast 解决办法:
//自己写一个 吐司的工具类
public class UIUtils {

public static void showToast(final Activity context,final String msg){

//判断是否是主线程

if("main".equals(Thread.currentThread().getName())){

Toast.makeText(context, msg, 1).show();

}else{

context.runOnUiThread(new Runnable() {

@Override

public void run() {

Toast.makeText(context, msg, 1).show();

}

});

}

}

    可以试一下
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问