广播接收器检查互联网连接

时间:2012-07-17 09:12:11

标签: android networking broadcastreceiver

我正在尝试使用广播接收器检查是否有互联网,我没有收到错误,但我的应用程序无法正常工作,它没有通知互联网的存在..有人可以帮忙吗?这是我的代码......提前致谢

package com.example.internetconnection;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    BroadcastReceiver networkStateReceiver = new BroadcastReceiver() { 

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub

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

    }; 
    IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);         
    registerReceiver(networkStateReceiver, filter);  

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
 }


}

Androidmanifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.internetconnection"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
      <receiver android:name="com.example.internetconnection" 
      android:label="NetworkConnection"> 
    <intent-filter> 
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> 
    </intent-filter> 
    </receiver> 
</application>

activity_main.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.internetconnection"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
      <receiver android:name="com.example.internetconnection" 
      android:label="NetworkConnection"> 
    <intent-filter> 
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> 
    </intent-filter> 
    </receiver> 
</application>

0 个答案:

没有答案