我正在建立一个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(); } } }更多推荐
发布评论