我如何检查移动数据连接并为其设置事件?

问题描述 投票:0回答:1

我想检查移动数据连接,如果打开或关闭,我将为其定义一个事件。数据单元打开后,显示移动数据图标,关闭移动数据后,移动数据图标也将被隐藏!

java android connection listener mobile-data
1个回答
0
投票

确定您是否有互联网连接

ConnectivityManager cm =
        (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
                      activeNetwork.isConnectedOrConnecting();

确定互联网连接的类型

ConnectivityManager cm =
        (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isMetered = cm.isActiveNetworkMetered();

有关更多信息,请单击here

© www.soinside.com 2019 - 2024. All rights reserved.