android判斷程序是否連接網絡

android判斷程序是否連接網絡。

> 判斷程序是否連接網絡還需要在XML中添加數據
>   
1./** 
2. * 檢測網絡是否連接 
3.     * @return 
4.     */  
5.    private boolean checkNetworkState() {  
6.        boolean flag = false;  
7.        //獲取連接管理實例 
8.        ConnectivityManager manager= (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
9.        //獲取當前網絡狀態
10.       NetworkInfo info = manager.getActiveNetworkInfo();
9.        //去進行判斷網絡是否連接   
13.        if (info != null && info.isAvailable()) {  
14.              isNetworkAvailable();  
15.        } else {  
16.             setNetwork();
17.        }  
18.  
19.        return flag;  
20.    }  
21.      
22.  
23.    /** 
24.     * 網絡未連接時,調用設置方法 
25.     */  
26.    private void setNetwork(){  
27.        Toast.makeText(this, "wifi is closed!", Toast.LENGTH_SHORT).show();  
28.          
29.        AlertDialog.Builder builder = new AlertDialog.Builder(this);  
30.        builder.setIcon(R.drawable.ic_launcher);  
31.        builder.setTitle("網絡提示信息");  
32.        builder.setMessage("網絡不可用,如果繼續,請先設置網絡!");  
33.        builder.setPositiveButton("設置", new OnClickListener() {  
34.            @Override  
35.            public void onClick(DialogInterface dialog, int which) {  
36.                Intent intent = null;  
37.                /** 
38.                 * 判斷手機系統的版本!如果API大於10 就是3.0+ 
39.                 * 因為3.0以上的版本的設置和3.0以下的設置不一樣,調用的方法不同 
40.                 */  
41.                if (android.os.Build.VERSION.SDK_INT > 10) {  
42.                    intent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);  
43.                } else {  
44.                    intent = new Intent();  
45.                    ComponentName component = new ComponentName(  
46.                            "com.android.settings",  
47.                            "com.android.settings.WirelessSettings");  
48.                    intent.setComponent(component);  
49.                    intent.setAction("android.intent.action.VIEW");  
50.                }  
51.                startActivity(intent);  
52.            }  
53.        });  
54.  
55.        builder.setNegativeButton("取消", new OnClickListener() {  
56.            @Override  
57.            public void onClick(DialogInterface dialog, int which) {  
58.  
59.            }  
60.        });  
61.        builder.create();  
62.        builder.show();  
63.    }  
64.      
65.    /** 
66.     * 網絡已經連接,然後去判斷是wifi連接還是GPRS連接 
67.     * 設置一些自己的邏輯調用 
68.     */  
69.    private void isNetworkAvailable(){  
70.          
71.        State gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();  
72.        State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();  
73.        if(gprs == State.CONNECTED || gprs == State.CONNECTING){  
74.            Toast.makeText(this, "wifi is open! gprs", Toast.LENGTH_SHORT).show();  
75.        }  
76.        //判斷為wifi狀態下才加載廣告,如果是GPRS手機網絡則不加載!  
77.        if(wifi == State.CONNECTED || wifi == State.CONNECTING){  
78.            Toast.makeText(this, "wifi is open! wifi", Toast.LENGTH_SHORT).show();  
79.            loadAdmob();  
80.        }  
81.          
82.    }  
83.      
84.    /** 
85.     * 在wifi狀態下 加載admob廣告 
86.     */  
87.    private void loadAdmob(){  
88.        ll = (LinearLayout) findViewById(R.id.load_ads);  
89.        ll.removeAllViews();  
90.        adsView = new AdView(this, AdSize.BANNER, "a15194a1ac9505d");  
91.        ll.addView(adsView);  
92.      
93.        adsView.loadAd(new AdRequest());  
94.    }  
北京假體隆鼻:https://www.fnxyey.com

> 判斷程序是否連接網絡還需要在XML中添加數據
>   
    

1./** 
2. * 檢測網絡是否連接 
3.     * @return 
4.     */  
5.    private boolean checkNetworkState() {  
6.        boolean flag = false;  
7.        //獲取連接管理實例 
8.        ConnectivityManager manager= (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
9.        //獲取當前網絡狀態
10.       NetworkInfo info = manager.getActiveNetworkInfo();
9.        //去進行判斷網絡是否連接   
13.        if (info != null && info.isAvailable()) {  
14.              isNetworkAvailable();  
15.        } else {  
16.             setNetwork();
17.        }  
18.  
19.        return flag;  
20.    }  
21.      
22.  
23.    /** 
24.     * 網絡未連接時,調用設置方法 
25.     */  
26.    private void setNetwork(){  
27.        Toast.makeText(this, "wifi is closed!", Toast.LENGTH_SHORT).show();  
28.          
29.        AlertDialog.Builder builder = new AlertDialog.Builder(this);  
30.        builder.setIcon(R.drawable.ic_launcher);  
31.        builder.setTitle("網絡提示信息");  
32.        builder.setMessage("網絡不可用,如果繼續,請先設置網絡!");  
33.        builder.setPositiveButton("設置", new OnClickListener() {  
34.            @Override  
35.            public void onClick(DialogInterface dialog, int which) {  
36.                Intent intent = null;  
37.                /** 
38.                 * 判斷手機系統的版本!如果API大於10 就是3.0+ 
39.                 * 因為3.0以上的版本的設置和3.0以下的設置不一樣,調用的方法不同 
40.                 */  
41.                if (android.os.Build.VERSION.SDK_INT > 10) {  
42.                    intent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);  
43.                } else {  
44.                    intent = new Intent();  
45.                    ComponentName component = new ComponentName(  
46.                            "com.android.settings",  
47.                            "com.android.settings.WirelessSettings");  
48.                    intent.setComponent(component);  
49.                    intent.setAction("android.intent.action.VIEW");  
50.                }  
51.                startActivity(intent);  
52.            }  
53.        });  
54.  
55.        builder.setNegativeButton("取消", new OnClickListener() {  
56.            @Override  
57.            public void onClick(DialogInterface dialog, int which) {  
58.  
59.            }  
60.        });  
61.        builder.create();  
62.        builder.show();  
63.    }  
64.      
65.    /** 
66.     * 網絡已經連接,然後去判斷是wifi連接還是GPRS連接 
67.     * 設置一些自己的邏輯調用 
68.     */  
69.    private void isNetworkAvailable(){  
70.          
71.        State gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();  
72.        State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();  
73.        if(gprs == State.CONNECTED || gprs == State.CONNECTING){  
74.            Toast.makeText(this, "wifi is open! gprs", Toast.LENGTH_SHORT).show();  
75.        }  
76.        //判斷為wifi狀態下才加載廣告,如果是GPRS手機網絡則不加載!  
77.        if(wifi == State.CONNECTED || wifi == State.CONNECTING){  
78.            Toast.makeText(this, "wifi is open! wifi", Toast.LENGTH_SHORT).show();  
79.            loadAdmob();  
80.        }  
81.          
82.    }  
83.      
84.    /** 
85.     * 在wifi狀態下 加載admob廣告 
86.     */  
87.    private void loadAdmob(){  
88.        ll = (LinearLayout) findViewById(R.id.load_ads);  
89.        ll.removeAllViews();  
90.        adsView = new AdView(this, AdSize.BANNER, "a15194a1ac9505d");  
91.        ll.addView(adsView);  
92.      
93.        adsView.loadAd(new AdRequest());  
94.    }

You May Also Like