腾讯QQ在没联网的情况下,界面效果图如下:
可以看到标题部分的提示:当前网络不可用,请检查网络设置。
这种效果可以添加到自己项目当中,于是写了一个Demo,效果图如下:
这种提示的弹出是通过BroadcastReceiver来实现的,判断当前网络是否连接正常。正常则不显示网络连接失败的提示,反之则如上图所示。
实现代码如下,首先我们来实现一下这个提示网络连接失败的布局文件
net_status_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/net_status_bar_top"
android:layout_width="fill_parent"
android:clickable="true"
android:layout_height="40.0dip"
android:background="@drawable/net_warning_bar_bg"
android:gravity="center_vertical"
android:paddingLeft="10.0dip"
android:visibility="gone" >
<TextView
android:id="@+id/net_status_bar_info_top"
android:layout_width="0.0dip"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:layout_marginRight="5.0dip"
android:layout_weight="1.0"
android:drawableLeft="@drawable/net_warning"
android:drawablePadding="10.0dip"
android:gravity="center_vertical"
android:text="@string/net_error_tip"
android:textColor="#ff2a271e"
android:textSize="13.0sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4.0dip"
android:src="@drawable/net_warning_redirect" />
</LinearLayout>
该布局文件所涉及到的图片资源:
,
,
添加一个检测当前网络是否正常的工具类:
NetUtil.java
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
public class NetUtil {
public static boolean isNetConnected(Context context) {
boolean isNetConnected;
// 获得网络连接服务
ConnectivityManager connManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connManager.getActiveNetworkInfo();
if (info != null && info.isAvailable()) {
isNetConnected = true;
} else {
Log.i("--->>", "没有可用网络");
isNetConnected = false;
}
return isNetConnected;
}
}
然后添加广播,用于自动检测当前网络是否连接正常。实现代码如下:
NetWorkReceiver.java
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.example.util.NetUtil;
/**
*
* @author Administrator
*
*/
@SuppressLint("NewApi")
public class NetWorkReceiver extends BroadcastReceiver {
public static ArrayList<EventHandler> ehList = new ArrayList<EventHandler>();
public static abstract interface EventHandler {
public abstract void onNetChange(boolean isNetConnected);
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.net.conn.CONNECTIVITY_CHANGE")) {
boolean isNetConnected = NetUtil.isNetConnected(context);
for (int i = 0; i < ehList.size(); i++)
((EventHandler) ehList.get(i)).onNetChange(isNetConnected);
}
}
}
接下来的实现代码就是用来调用的主页面代码:
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import com.example.broadcast.NetWorkReceiver;
import com.example.util.NetUtil;
public class MainActivity extends Activity implements OnClickListener,
NetWorkReceiver.EventHandler {
private View mNetErrorView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNetErrorView = findViewById(R.id.net_status_bar_top);
mNetErrorView.setOnClickListener(this);
}
public void onClickNet(View v) {
if (!NetUtil.isNetConnected(this)) {
Toast.makeText(getApplicationContext(), R.string.net_error_tip, 1)
.show();
return;
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.net_status_bar_top:
// 跳转到网络设置
startActivity(new Intent(
android.provider.Settings.ACTION_WIFI_SETTINGS));
break;
default:
break;
}
}
@Override
public void onNetChange(boolean isNetConnected) {
if (!isNetConnected) {
Toast.makeText(getApplicationContext(), R.string.net_error_tip, 1)
.show();
mNetErrorView.setVisibility(View.VISIBLE);
} else {
mNetErrorView.setVisibility(View.GONE);
}
}
@Override
protected void onResume() {
super.onResume();
if (!NetUtil.isNetConnected(this))
mNetErrorView.setVisibility(View.VISIBLE);
else
mNetErrorView.setVisibility(View.GONE);
}
}
好的,大功告成,窃喜的同时有没有想想还少了点什么东西?对,我们写的广播还没在清单文件里面注册,下面就添加上去。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.net"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.net.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.broadcast.NetWorkReceiver" >
<intent-filter>
<!-- 接收网络变化的广播 -->
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>
OK,讲解到此结束。