日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

android程序如何联网,如何判断软件程序是否联网 联网状态提示信息Android实现

發(fā)布時(shí)間:2023/12/20 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android程序如何联网,如何判断软件程序是否联网 联网状态提示信息Android实现 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在項(xiàng)目中,經(jīng)常需要判斷是否有網(wǎng)絡(luò)連接。最近學(xué)習(xí)了如何判斷軟件是否聯(lián)網(wǎng),如果沒有聯(lián)網(wǎng),彈出提示信息,連接網(wǎng)絡(luò)。

效果:

(1)聯(lián)網(wǎng)情況下:

(2)不聯(lián)網(wǎng)情況下:

(3)點(diǎn)擊“檢測(cè)設(shè)置”:

判斷我們的軟件是否聯(lián)網(wǎng),看代碼吧:

/**

* 檢測(cè)網(wǎng)絡(luò)連接

*

* @param con

* @return

*/

public boolean isNetworkAvailable(Context con) {

ConnectivityManager cm = (ConnectivityManager) con

.getSystemService(Context.CONNECTIVITY_SERVICE);

if (cm == null)

return false;

NetworkInfo netinfo = cm.getActiveNetworkInfo();

if (netinfo == null) {

return false;

}

if (netinfo.isConnected()) {

return true;

}

return false;

}

如果沒有聯(lián)網(wǎng),彈出提示框,提示設(shè)置網(wǎng)絡(luò)連接:

/**

* 提示設(shè)置網(wǎng)絡(luò)連接對(duì)話框

*

* @param context

*/

public void showNetDialog(final Context context) {

mMaterialDialog = new MaterialDialog(context)

.setMessage("世界上最遙遠(yuǎn)的距離就是沒網(wǎng)")

.setPositiveButton("檢查設(shè)置", new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = null;

try {

@SuppressWarnings("deprecation")

String sdkVersion = android.os.Build.VERSION.SDK;

if (Integer.valueOf(sdkVersion) > 10) {

intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);

} else {

intent = new Intent();

ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");

intent.setComponent(comp);

intent.setAction("android.intent.action.VIEW");

}

context.startActivity(intent);

} catch (Exception e) {

e.printStackTrace();

}

mMaterialDialog.dismiss();

}

}).setNegativeButton("取消", new View.OnClickListener() {

@Override

public void onClick(View v) {

mMaterialDialog.dismiss();

}

});

mMaterialDialog.show();

}

怎么做判斷處理:

/**

* 判斷是否聯(lián)網(wǎng)

*/

private void checkNet() {

if(!mCheckNetWork.isNetworkAvailable(getApplication())){

mCheckNetWork.showNetDialog(MainActivity.this);

}else {

Toast.makeText(MainActivity.this,

"有網(wǎng)絡(luò),哈哈",Toast.LENGTH_SHORT).show();

}

}

具體代碼如下:

MainActivity.java

package com.bzu.gxs.chectnetwork;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener{

private CheckNetWork mCheckNetWork =new CheckNetWork();

private Button btn_check;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

init();

}

/**

* 點(diǎn)擊事件

*

* @param view

*/

@Override

public void onClick(View view) {

switch (view.getId()){

case R.id.btn_check:

checkNet();

}

}

/**

* 判斷是否聯(lián)網(wǎng)

*/

private void checkNet() {

if(!mCheckNetWork.isNetworkAvailable(getApplication())){

mCheckNetWork.showNetDialog(MainActivity.this);

}else {

Toast.makeText(MainActivity.this,

"有網(wǎng)絡(luò),哈哈",Toast.LENGTH_SHORT).show();

}

}

/**

* 初始化

*/

private void init() {

btn_check = (Button) findViewById(R.id.btn_check);

btn_check.setOnClickListener(this);

}

}

CheckNetWork.java

package com.bzu.gxs.chectnetwork;

import android.content.ComponentName;

import android.content.Context;

import android.content.Intent;

import android.media.browse.MediaBrowser;

import android.net.ConnectivityManager;

import android.net.NetworkInfo;

import android.os.Build;

import android.view.View;

import me.drakeet.materialdialog.MaterialDialog;

/**

* 網(wǎng)絡(luò)檢測(cè)

* Created by GXS on 2016/5/13.

*/

public class CheckNetWork {

private MaterialDialog mMaterialDialog;

/**

* 檢測(cè)網(wǎng)絡(luò)連接

*

* @param con

* @return

*/

public boolean isNetworkAvailable(Context con) {

ConnectivityManager cm = (ConnectivityManager) con

.getSystemService(Context.CONNECTIVITY_SERVICE);

if (cm == null)

return false;

NetworkInfo netinfo = cm.getActiveNetworkInfo();

if (netinfo == null) {

return false;

}

if (netinfo.isConnected()) {

return true;

}

return false;

}

/**

* 提示設(shè)置網(wǎng)絡(luò)連接對(duì)話框

*

* @param context

*/

public void showNetDialog(final Context context) {

mMaterialDialog = new MaterialDialog(context)

.setMessage("世界上最遙遠(yuǎn)的距離就是沒網(wǎng)")

.setPositiveButton("檢查設(shè)置", new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = null;

try {

@SuppressWarnings("deprecation")

String sdkVersion = android.os.Build.VERSION.SDK;

if (Integer.valueOf(sdkVersion) > 10) {

intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);

} else {

intent = new Intent();

ComponentName comp = new

ComponentName("com.android.settings",

"com.android.settings.WirelessSettings");

intent.setComponent(comp);

intent.setAction("android.intent.action.VIEW");

}

context.startActivity(intent);

} catch (Exception e) {

e.printStackTrace();

}

mMaterialDialog.dismiss();

}

}).setNegativeButton("取消", new View.OnClickListener() {

@Override

public void onClick(View v) {

mMaterialDialog.dismiss();

}

});

mMaterialDialog.show();

}

}

activity_main.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.bzu.gxs.chectnetwork.MainActivity">

android:id="@+id/btn_check"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="測(cè)試"/>

注意:需要在清單文件AndroidManifest.xml中加入

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持。

總結(jié)

以上是生活随笔為你收集整理的android程序如何联网,如何判断软件程序是否联网 联网状态提示信息Android实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。