看起来像API 29上的整个NetworkInfo is deprecated。
因此,我正在寻找一种替代方法来检查网络是否已连接。 例如。替代
connectivityManager.activeNetworkInfo?.isConnected == true
旁注:我知道现在有一个回调,但是我想同步获取此信息。另外,我当然知道,虽然我想获得它可能不是准确的信息。
答案 0 :(得分:0)
这正在使用API 29:
connectivityManager.isDefaultNetworkActive
答案 1 :(得分:0)
private function _get_datatables_query($table,$column_order='',$column_search='',$order1='',$field_arry='',$select='')
{
if(!empty($select)) {
$this->db->select($select);
}
$this->db->from($table);
if (!empty($field_arry) && count($field_arry) > 0 && $table != '') {
foreach ($field_arry as $key => $value) {
$this->db->where($key, $value);
}
}
$i = 0;
// print_r($column_search);
$names='';
// echo $_POST['search']['value'];
if(!empty($_POST['search']['value'])){
$names = explode(' ',$_POST['search']['value']);
}
// print_r($names);
foreach ($column_search as $key => $item) // loop column
{
if($_POST['search']['value']) // if datatable send POST for search
{
if($i===0) // first loop
{
$this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can
combine with other WHERE with AND.
if(count($names) > 1) {
$this->db->like("CONCAT(CONCAT('%',$item),'%')", $names[$key]);
}else{
$this->db->or_like("CONCAT(CONCAT('%',$item),'%')",
$_POST['search']['value']);
}
}
else
{
if(count($names) > 1) {
$this->db->or_like("CONCAT(CONCAT('%',$item),'%')", $names[$key]);
}else{
$this->db->or_like("CONCAT(CONCAT('%',$item),'%')",
$_POST['search']['value']);
}
}
if(count($column_search) - 1 == $i) //last loop
$this->db->group_end(); //close bracket
}
$i++;
}
if(isset($_POST['order'])) // here order processing
{
$this->db->order_by($column_order[$_POST['order']['0']['column']],
$_POST['order']['0']['dir']);
}
else if(isset($order1))
{
$order = $order1;
$this->db->order_by(key($order), $order[key($order)]);
}
}
function get_datatables($table,$column_order='',$column_search='',$order1='',$field_array='',$join_array='',$select='')
{
$this->_get_datatables_query($table,$column_order,$column_search,$order1,$field_array,$select);
// $this->output->enable_profiler(TRUE);
if($_POST['length'] != -1)
$this->db->limit($_POST['length'], $_POST['start']);
if (!empty($join_array) && count($join_array) > 0 && $table != '') {
foreach ($join_array as $key => $value) {
$this->db->join($key, $value);
}
}
$query = $this->db->get();
return $query->result();
}
该类已在API级别29中弃用。
相反,您应该使用android.net.NetworkInfo
API来了解连通性更改
参考链接https://developer.android.com/reference/android/net/NetworkInfo.html
ConnectivityManager.NetworkCallback
答案 2 :(得分:0)
解决方案是这样的:
val capability = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
return capability?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) ?: false
答案 3 :(得分:0)
解决方案是
public void registerNetworkCallback()
{
try {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
connectivityManager.registerNetworkCallback(builder.build(),new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
Variables.isNetworkConnected = true; // Global Static Variable
}
@Override
public void onLost(Network network) {
Variables.isNetworkConnected = false; // Global Static Variable
}
}
);
Variables.isNetworkConnected = false;
}catch (Exception e){
Variables.isNetworkConnected = false;
}
}
在此处查看完整代码:Gist
答案 4 :(得分:0)
private boolean checkInternetConnection(Activity activity) {
ConnectivityManager connectivityManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Network network = null;
if (connectivityManager == null) {
return false;
} else {
network = connectivityManager.getActiveNetwork();
NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(network);
if (networkCapabilities == null) {
return false;
}
if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
return true;
}
}
} else {
if (connectivityManager == null) {
return false;
}
if (connectivityManager.getActiveNetworkInfo() == null) {
return false;
}
return connectivityManager.getActiveNetworkInfo().isConnected();
}
return false;
}