Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android手机的服务状态_Android_Networking - Fatal编程技术网

android手机的服务状态

android手机的服务状态,android,networking,Android,Networking,在我的一个应用程序中,我想检查android手机的服务状态 在发送短信之前。我使用了下面的代码来实现这一点 //check service ServiceState pstate = new ServiceState(); if(pstate.getState() != ServiceState.STATE_IN_SERVICE) { Log.v(TAG,"service state" +pstate.getStat

在我的一个应用程序中,我想检查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;
        }
但是代码总是返回时没有服务(in+pstate.getState中的值为1)

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


这段代码是在FROYO版本中检查的。

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

但是使用这个方法,效果非常好。对于您的情况,我建议使用包装器,而不是直接实例化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中,并仅在实际调用时(如果有)注册它,并且仅在服务可用时保存。即

    //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;
    }
但是请注意,这将要求侦听器在注册后立即被调用,否则您将得到任意结果——因此请确保检查这一点