【问题标题】:Instant signal strength即时信号强度
【发布时间】:2011-04-22 18:32:40
【问题描述】:

我需要想办法测量Android手机当前的信号强度,而不需要注册一个PhoneStateListener,天知道什么时候返回实际的asu。

类似:

int signal = getPhoneSignal();

有什么帮助吗?

谢谢!

【问题讨论】:

  • 你可以试试我的解决方案,看看它是否有效?

标签: android signal-strength


【解决方案1】:

如果您仔细查看 Android 资源,您会发现在注册 PhoneStateListener 后,您将收到即时通知:

public void listen(PhoneStateListener listener, int events) {
        String pkgForDebug = mContext != null ? mContext.getPackageName() : "<unknown>";
        try {
            Boolean notifyNow = (getITelephony() != null);
            mRegistry.listen(pkgForDebug, listener.callback, events, notifyNow);
        } catch (RemoteException ex) {
            // system process dead
        }
    }

因此,您可以创建自己的计时器并在计时器更新时注册新的侦听器,并在收到即时更新后通过传递相同的侦听器对象将其删除,并将 events 参数设置为 LISTEN_NONE

当然我不能称之为最佳实践,但我能看到的唯一选择是根据getNeighboringCellInfo() 的信号强度自行计算信号强度。

附言Not only God knowsPhoneStateListener 被触发时;)

【讨论】:

    【解决方案2】:

    我不认为有办法直接做到这一点。但是您可以注册 PhoneStateListener 并将最后更新的值保存到变量中并返回/调用它。

    【讨论】:

    • 我试图这样做:从主线程调用 Activity PhoneStateListener 从主线程更新我无法让 Activity 等待 PhoneStateListener 保存其第一个信号值...我遇到了死锁:S
    【解决方案3】:
    class Signal {
    
        static volatile CountDownLatch latch;
        static int asu;
        private final static String TAG = Signal.class.getName();
    
        int getSignalStrength(Context ctx) throws InterruptedException {
            Intent i = new Intent(TAG + ".SIGNAL_ACTION", Uri.EMPTY, ctx,
                    SignalListenerService.class);
            latch = new CountDownLatch(1);
            asu = -1;
            ctx.startService(i);
            Log.w(TAG, "I wait");
            latch.await();
            ctx.stopService(i);
            return asu;
        }
    }
    

    在哪里:

    public class SignalListenerService extends Service {
    
        private TelephonyManager Tel;
        private SignalListener listener;
        private final static String TAG = SignalListenerService.class.getName();
    
        private static class SignalListener extends PhoneStateListener {
    
            private volatile CountDownLatch latch;
    
            private SignalListener(CountDownLatch la) {
                Log.w(this.getClass().getCanonicalName(), "CSTOR");
                this.latch = la;
            }
    
            @Override
            public void onSignalStrengthChanged(int asu) {
                Signal.asu = asu;
                latch.countDown();
            }
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.w(TAG, "Received : " + intent.getAction());
            Tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            listener = new SignalListener(Signal.latch);
            @SuppressWarnings("deprecation")
            final int listenSs = PhoneStateListener.LISTEN_SIGNAL_STRENGTH;
            Tel.listen(listener, listenSs);
            return START_STICKY;
        }
    
        @Override
        public void onDestroy() {
            Log.w(TAG, "onDestroy");
            Tel.listen(listener, PhoneStateListener.LISTEN_NONE);
            super.onDestroy();
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    }
    

    这是工作代码。不要忘记在清单中注册您的服务并获取权限。这样做可能有更好/更优雅的方法,因此欢迎 cmets/更正。

    【讨论】:

      猜你喜欢
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      • 2015-10-25
      • 1970-01-01
      • 2011-03-10
      相关资源
      最近更新 更多