android 监听wifi和数据连接

我们项目现在改登录流程,是在wifi或者数据连接开启并且连接到网络的时候,来后台自动注册和登录账户。这样好像可以增加用户数。

我的实现,是通过广播BroadCastReciver触发器组件来实现,在项目中,为了全局性,实在服务中注册的,测出的demo只是在应用中的,原理都一样,只是注册的地方不同而已。


代码如下:

package com.example.netlistener;

import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.view.Menu;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		BroadcastReceiver receiver=new NetBroadCastReciver();
		
		//注册BroadCastReciver,设置监听的频道。就是filter中的
		IntentFilter filter=new IntentFilter();
		filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
		filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
		filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		registerReceiver(receiver, filter);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}

package com.example.netlistener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Parcelable;
import android.util.Log;

public class NetBroadCastReciver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		//判断wifi是打开还是关闭
		if(WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())){ //此处无实际作用,只是看开关是否开启
			int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
			switch (wifiState) {
			case WifiManager.WIFI_STATE_DISABLED:
				break;

			case WifiManager.WIFI_STATE_DISABLING:
				break;
			}
		}
		//此处是主要代码,
		//如果是在开启wifi连接和有网络状态下
		if(ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())){
			ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
			if(NetworkInfo.State.CONNECTED==info.getState()){
				//连接状态
				Log.e("pzf", "有网络连接");
				//执行后续代码
				//new AutoRegisterAndLogin().execute((String)null);
				//ps:由于boradCastReciver触发器组件,他和Service服务一样,都是在主线程的,所以,如果你的后续操作是耗时的操作,请new Thread获得AsyncTask等,进行异步操作
			}else{
				Log.e("pzf", "无网络连接");
			}
		}
		
		
		
		
	}

}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.netlistener"
    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.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.netlistener.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=".NetBroadCastReciver"
            >
             <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
                <action android:name="android.net.wifi.STATE_CHANGE" />
            </intent-filter>
            
            
        </receiver>
    </application>

</manifest>


源码地址:

http://download.csdn.net/detail/pangzaifei/6938315


android 监听wifi和数据连接

上一篇:Android自动化测试(Jenkins+Robotium+Ant+Junit)[PART TWO]


下一篇:Android Notification