有没有办法检查4g网络是否在Android上稳定?(Is there any way to check if 4g network is stabilized on Android?)

编程入门 行业动态 更新时间:2024-10-27 11:15:38
没有办法检查4g网络是否在Android上稳定?(Is there any way to check if 4g network is stabilized on Android?)

我正在建立一个VoIP客户端,我想让客户端拨打连接4G时拨打的最后一个电话号码。 如果用户按下“结束通话”按钮,它不会自动呼叫。 只有在连接断开且用户无法按结束通话才能完成电话呼叫时才会发生这种情况。

所以我有一个BroadcastReceiver ,它告诉我互联网连接是否可用或不是很好。 除了它告诉我太多了。

我看到手机的行为是当没有网络并且它从4G网络获得连接时, BroadcastReceiver onReceive(..)方法被触发大约8次,[连接,未连接,连接,未连接,......,连接的]。

这是侦听连接的代码。

@Override public void onReceive( Context context, Intent intent ) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE ); TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); NetworkInfo an = cm.getActiveNetworkInfo(); if ( an != null ) { // this method just decides if it is WIFI or fast network like // 4G or HSPAP. state = updateConnectionAvailability(context, an, tm); // 1: 4G/HSPAP, 3: WIFI if (state == 1 || state == 3) { Toast.makeText( context, "Fast network available", Toast.LENGTH_SHORT ).show(); } else { Toast.makeText( context, "Warning: Slow network available", Toast.LENGTH_SHORT ).show(); } if (mCCL != null) { mCCL.onVoipAvailable(); } } else { Toast.makeText( context, "There is no Internet connection!", Toast.LENGTH_SHORT).show(); if (mCCL != null) { mCCL.onVoipNotAvailable(); } } }

你可能已经注意到了,我创建了一个自定义Listener mCCL。 它只是通知与MainActivity的连接。 但onVoipAvailable()回来,有一系列onVoipAvailable()和onVoipNotAvailable()一旦建立4G就被调用。

我的目标是找出4G是否建立得相当好。 这是非常模糊的,但我不能用不同的方式来说。 :-(

我该怎么办?

I am making a VoIP client and I wanted to make the client to call to the last phone number it was talking to when 4G is connected. It doesn't automatically call if user pressed "end call" button. It only happens if connection was dropped and user couldn't press end call to finish the phone call.

So I have a BroadcastReceiver and it tells me if Internet connection is available or not pretty well. Except it tells me too much.

The phone's behavior I saw was when there's no network and it gets the connectivity from 4G network, onReceive(..) method in BroadcastReceiver is triggered about 8 times, with [connected, NOT connected, connected, NOT connected,...., connected].

Here's the code that listens to the connectivity.

@Override public void onReceive( Context context, Intent intent ) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE ); TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); NetworkInfo an = cm.getActiveNetworkInfo(); if ( an != null ) { // this method just decides if it is WIFI or fast network like // 4G or HSPAP. state = updateConnectionAvailability(context, an, tm); // 1: 4G/HSPAP, 3: WIFI if (state == 1 || state == 3) { Toast.makeText( context, "Fast network available", Toast.LENGTH_SHORT ).show(); } else { Toast.makeText( context, "Warning: Slow network available", Toast.LENGTH_SHORT ).show(); } if (mCCL != null) { mCCL.onVoipAvailable(); } } else { Toast.makeText( context, "There is no Internet connection!", Toast.LENGTH_SHORT).show(); if (mCCL != null) { mCCL.onVoipNotAvailable(); } } }

as you may have noticed, I created a custom Listener mCCL. It simply notifies the connectivity to the MainActivity. But then again, there is a series of onVoipAvailable() and onVoipNotAvailable() called once 4G is established.

My goal is to find out if 4G is established pretty well. It is very vague, but I can't word it differently. :-(

What should I do?

最满意答案

在我的应用程序中,我确实避免重复广播。 但是,我收到的序列是同一网络的几个“连接”,或同一网络的几个“断开连接”:

public NetworkInfo previous_network = null; @Override public void onReceive( Context context, Intent intent ) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE ); TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); NetworkInfo an = cm.getActiveNetworkInfo(); ///AVOID EXTRA BROADCAST if (previous_network == null && an==null) { Log.i(mTag, "detected duplicate broadcast - no network"); return; } if (previous_network != null && an != null) { if (an.toString().compareTo(previous_network.toString()) == 0) { Log.i(mTag, "detected duplicate broadcast - no change"); return; } } previous_network = an; ///END OF AVOID EXTRA BROADCAST if ( an != null ) { // this method just decides if it is WIFI or fast network like // 4G or HSPAP. state = updateConnectionAvailability(context, an, tm); // 1: 4G/HSPAP, 3: WIFI if (state == 1 || state == 3) { Toast.makeText( context, "Fast network available", Toast.LENGTH_SHORT ).show(); } else { Toast.makeText( context, "Warning: Slow network available", Toast.LENGTH_SHORT ).show(); } if (mCCL != null) { mCCL.onVoipAvailable(); } } else { Toast.makeText( context, "There is no Internet connection!", Toast.LENGTH_SHORT).show(); if (mCCL != null) { mCCL.onVoipNotAvailable(); } } }

In my app, I do avoid duplicate broadcast. However, the sequence I receive is several "connected" for the same network, or several "disconnected" for the same network:

public NetworkInfo previous_network = null; @Override public void onReceive( Context context, Intent intent ) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE ); TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); NetworkInfo an = cm.getActiveNetworkInfo(); ///AVOID EXTRA BROADCAST if (previous_network == null && an==null) { Log.i(mTag, "detected duplicate broadcast - no network"); return; } if (previous_network != null && an != null) { if (an.toString().compareTo(previous_network.toString()) == 0) { Log.i(mTag, "detected duplicate broadcast - no change"); return; } } previous_network = an; ///END OF AVOID EXTRA BROADCAST if ( an != null ) { // this method just decides if it is WIFI or fast network like // 4G or HSPAP. state = updateConnectionAvailability(context, an, tm); // 1: 4G/HSPAP, 3: WIFI if (state == 1 || state == 3) { Toast.makeText( context, "Fast network available", Toast.LENGTH_SHORT ).show(); } else { Toast.makeText( context, "Warning: Slow network available", Toast.LENGTH_SHORT ).show(); } if (mCCL != null) { mCCL.onVoipAvailable(); } } else { Toast.makeText( context, "There is no Internet connection!", Toast.LENGTH_SHORT).show(); if (mCCL != null) { mCCL.onVoipNotAvailable(); } } }

更多推荐

本文发布于:2023-07-24 08:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1243471.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有办法   稳定   网络   stabilized   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!