Android開發網絡鏈接

Android中判斷網絡是否鏈接代碼:java

mainActivity:
倒包狀況:android

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {網絡

    //檢測網絡鏈接狀態
    private ConnectivityManager manager;
    private LinearLayout ll;
    private AdView adsView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }app

    /**
     * 檢測網絡是否鏈接
     * @return
     */
    private boolean checkNetworkState() {
        boolean flag = false;
        //獲得網絡鏈接信息
        manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        //去進行判斷網絡是否鏈接
        if (manager.getActiveNetworkInfo() != null) {
            flag = manager.getActiveNetworkInfo().isAvailable();
        }
        if (!flag) {
            setNetwork();
        } else {
//            isNetworkAvailable();
        }less

        return flag;
    }ide

    /**
     * 網絡未鏈接時,調用設置方法
     */
    private void setNetwork(){
        Toast.makeText(this, "wifi is closed!", Toast.LENGTH_SHORT).show();
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_launcher);
        builder.setTitle("網絡提示信息");
        builder.setMessage("網絡不可用,若是繼續,請先設置網絡!");
        builder.setPositiveButton("設置", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = null;
                /**
                 * 判斷手機系統的版本!若是API大於10 就是3.0+
                 * 由於3.0以上的版本的設置和3.0如下的設置不同,調用的方法不一樣
                 */
                if (android.os.Build.VERSION.SDK_INT > 10) {
                    intent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
                } else {
                    intent = new Intent();
                    ComponentName component = new ComponentName(
                            "com.android.settings",
                            "com.android.settings.WirelessSettings");
                    intent.setComponent(component);
                    intent.setAction("android.intent.action.VIEW");
                }
                startActivity(intent);
            }
        });ui

        builder.setNegativeButton("取消", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {this

            }
        });
        builder.create();
        builder.show();
    }
    /**
     * 網絡已經鏈接,而後去判斷是wifi鏈接仍是GPRS鏈接
     * 設置一些本身的邏輯調用
     */
    private void isNetworkAvailable(){
        State gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        if(gprs == State.CONNECTED || gprs == State.CONNECTING){
            Toast.makeText(this, "wifi is open! gprs", Toast.LENGTH_SHORT).show();
        }
        //判斷爲wifi狀態下才加載廣告,若是是GPRS手機網絡則不加載!
        if(wifi == State.CONNECTED || wifi == State.CONNECTING){
            Toast.makeText(this, "wifi is open! wifi", Toast.LENGTH_SHORT).show();
            loadAdmob();
        }
    }
    /**
     * 在wifi狀態下 加載admob廣告
     */
    private void loadAdmob(){
        ll = (LinearLayout) findViewById(R.id.load_ads);
        ll.removeAllViews();
        adsView = new AdView(this, AdSize.BANNER, "a15194a1ac9505d");
        ll.addView(adsView);
        adsView.loadAd(new AdRequest());
    }
    @Override
    protected void onResume() {
        Log.i("lzan13", "onResume");
        checkNetworkState();
        super.onResume();
    }
    @Override
    protected void onDestroy() {
        Log.i("lzan13", "onDestroy");
        super.onDestroy();
    }google

}spa

在AndroidManifest.xml的權限設置:

<!-- 訪問網絡權限 -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- 檢測網絡狀態權限 -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
相關文章
相關標籤/搜索