不幸的是应用程序的同时检查网络越来越停止 [英] unfortunately app is getting stopped while checking for network
本文介绍了不幸的是应用程序的同时检查网络越来越停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用下面的code,以检查在我的应用程序的网络连接。
I am using the following code to check for the internet connection throughout my app.
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();
}
}
}
和我定义所需的权限清单文件
And I have defined required permission in the manifest file
每当我尝试断开/连接使用F8键,我会收到不幸的是APP已停止,而我没有得到在任何的logcat打印网络。 我能知道什么是错误的,我在做什么?
Whenever I try to disconnect/connect the network using F8 key I will receive "UNFORTUNATELY APP HAS STOPPED", and I am not getting any print in logcat. Can I know what is the mistake I am doing?
推荐答案
你有没有在的AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
如果您在上面添加,然后检查该code:
if you have added above then check this code:
<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);
}
}
这篇关于不幸的是应用程序的同时检查网络越来越停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文