0

在我的一个应用程序中,我想在发送短信之前检查 android 手机的服务状态。我已经使用以下代码来做到这一点

                //check service
    ServiceState pstate = new ServiceState();
    if(pstate.getState() != ServiceState.STATE_IN_SERVICE)
        {
        Log.v(TAG,"service state" +pstate.getState());
        Toast.makeText(Myactivity.this, "error string", 2000).show();
        return;
        }

但是代码总是返回 OUT_OF_SERVICE(+pstate.getState 中的值为 1)

请让我知道检查手机是否处于 STATE_IN_SERVICE 的可靠方法是什么?

此代码在 FROYO 版本中进行了检查。

4

1 回答 1

0

确实不是一个令人满意的答案,但我遇到了同样的问题并且一直在浪费时间,但它也不适用于我的 FROYO 版本。

但是使用TelephonyManagerPhoneStateListener效果很好。对于您的情况,我建议使用包装器而不是直接实例化 ServiceState ,即

    //declare current state
    ServiceState myServiceState = new ServiceState();
    PhoneStateListener listener = null; // not sure if this is needed really..


    // nifty getter
    public ServiceState getServiceState(){ return myServiceState; }


    //setup listener (eg. in onCreate)
    TelephonyManager tm = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
    listener = new PhoneStateListener() {
        @Override
        public void onServiceStateChanged(ServiceState serviceState){
            myServiceState = serviceState;
        }
    };
    tm.listen(listener,PhoneStateListener.LISTEN_SERVICE_STATE);


    // to be called when destroying your context
    public void unregisterListener(){
        // something like..
        tm.listen(listener,PhoneStateListener.LISTEN_NONE);
    }


    //check service
    ServiceState pstate = getServiceState();
    if(pstate.getState() != ServiceState.STATE_IN_SERVICE)
    {
        Log.v(TAG,"service state" +pstate.getState());
        Toast.makeText(Myactivity.this, "error string", 2000).show();
        return;
    }

一个更懒惰的解决方案是将监听器设置移动到 getter 中,并仅在实际调用时注册它,如果有的话,并且只有在服务可用时才保存。IE

    //declaration
    boolean isAvailable = false;
    PhoneStateListener listener = null;


    // more nifty getter
    public boolean isServiceAvailable(){
        if (listener == null){
            //setup listener if not yet done
            TelephonyManager tm = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
            listener = new PhoneStateListener() {
                @Override
                public void onServiceStateChanged(ServiceState serviceState){
                    isAvailable = serviceState.getState() == ServiceState.STATE_IN_SERVICE;
                }
            };
            tm.listen(listener,PhoneStateListener.LISTEN_SERVICE_STATE);
        }
        return isAvailable;
    }

    // to be called when destroying your context
    public void unregisterListener(){
        // something like..
        if (lister != null){
            tm.listen(listener,PhoneStateListener.LISTEN_NONE);
        }
    }



    //check service
    if(! isServiceAvailable())
    {
        Log.v(TAG,"service state" +pstate.getState());
        Toast.makeText(Myactivity.this, "error string", 2000).show();
        return;
    }

但请注意,这将要求侦听器在注册后立即被调用,否则您最终会得到任意结果 - 因此请务必检查。

于 2013-01-16T01:29:15.437 回答