在网络单元更改时未调用ACTION_PHONE_STATE_CHANGED [英] ACTION_PHONE_STATE_CHANGED not called on network cell changes

查看:385
本文介绍了在网络单元更改时未调用ACTION_PHONE_STATE_CHANGED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当电话进出接收区时,我正在尝试使广播接收器启动.问题是,当小区接收发生变化时,接收方永远不会被呼叫. BroadcastReceiver可以很好地用于获取电话状态(呼叫空闲,开始等),还可以用于打开和关闭飞行模式,因为广播接收器可以同时处理这两种情况.

I am trying to make my broadcast receiver fire when the phone goes in and out of reception areas. The issue is the receiver never gets called when the cell reception changes. The BroadcastReceiver works fine for getting phone call states (call idle, started ect...), and also for getting the airplane mode switched on and off because the broadcast receiver handles both.

我向清单中的接收者添加了权限和意图过滤器,它们工作正常.

I added the permissions and intent filter to the receiver in the manifest and they are working fine.

这是我的BroadcastReceiver和PhoneStateListener所拥有的.

Here is what I have for my BroadcastReceiver and PhoneStateListener.

public class PhoneStateReceiver extends BroadcastReceiver {

private PhoneStateListener mListener = new ServiceStateListener();
private TelephonyManager mTelephonyManager;
private Context mContext;
/**
 * TODO add some sort of call back interface to allow for different uses of this phone state receiver
 */

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    mContext = context;
    if (action.intern().equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
        boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
        if (!isAirplaneModeOn) {
            SmsRetryManager.getInstance().retryAllSms(context);
        }
    } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
        mTelephonyManager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        Toast.makeText(mContext, "Receiver registered!", Toast.LENGTH_LONG).show();
        mTelephonyManager.listen(mListener, PhoneStateListener.LISTEN_SERVICE_STATE);
        mTelephonyManager.listen(mListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    }
}


public void onDestroy() {
    mTelephonyManager.listen(mListener, PhoneStateListener.LISTEN_NONE);
}

private class ServiceStateListener extends PhoneStateListener {

    @Override
    public void onServiceStateChanged (ServiceState serviceState) {
        super.onServiceStateChanged(serviceState);
        boolean connected = (serviceState.getState() == ServiceState.STATE_IN_SERVICE);
        if (connected) {
            Toast.makeText(mContext, "Connection Gained!", Toast.LENGTH_LONG).show();
            //todo retry sms here
            SmsRetryManager.getInstance().retryAllSms(mContext);

        } else {
            Toast.makeText(mContext, "Connection Lost!", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        super.onSignalStrengthsChanged(signalStrength);
        Toast.makeText(mContext, "Signal changed - cdma : " + signalStrength.getCdmaDbm() + " gsm : " + signalStrength.getGsmSignalStrength(), Toast.LENGTH_LONG).show();
    }
}

任何见识都会很棒.我一直在这头上撞了一段时间.

Any insight would be awesome. I have been banging my head on this one for a while.

谢谢!

推荐答案

我假设您正在收听广播android.intent.action.SERVICE_STATE

I assume you are listening the broadcast android.intent.action.SERVICE_STATE

如果是这样,请尝试使用:

If so, try using:

if (TelephonyManager.getnetworkOperator.length()==0) connected=false;

在您的OnReceive方法上按

,以了解电话是否未连接.对我来说很好.

on your OnReceive method, to know if the phone is not connected. It works fine for me.

如果该解决方案不起作用,请说明如何注册接收者:它是在manifest.xml中静态注册的吗?或动态地使用PackageManager.setComponentEnabledSetting?注意:使用静态注册后,您会发现重新安装应用程序后未触发接收器,需要添加到接收器标签中

If this solution doesn't work, please show how you register the receiver: is it statically registered at manifest.xml? or dynamically with PackageManager.setComponentEnabledSetting? Note: With static registration you'll find the receiver is not triggered after reinstalling the app, needing to add to the receiver tag

<intent-filter> <action android:name= "android.intent.action.MY_PACKAGE_REPLACED"/></intent-filter>

<intent-filter> <action android:name= "android.intent.action.MY_PACKAGE_REPLACED"/></intent-filter>

您还可以查看返回ServiceState的值. 参见 http://developer.android.com/reference/android/telephony/ServiceState .html

You can also look at the values that return the ServiceState. See here http://developer.android.com/reference/android/telephony/ServiceState.html

并检查返回以下哪个值:

And check which of these values is returned:

int STATE_EMERGENCY_ONLY =电话已注册并锁定.仅有的 紧急电话是允许的.

int STATE_EMERGENCY_ONLY = The phone is registered and locked. Only emergency numbers are allowed.

int STATE_IN_SERVICE =正常运行状态,电话已在运营商在本地网络或漫游中注册.

int STATE_IN_SERVICE = Normal operation condition, the phone is registered with an operator either in home network or in roaming.

int STATE_OUT_OF_SERVICE =电话未在任何运营商处注册, 电话可能正在搜索新的运营商以注册,或者 根本不搜索注册,或者注册被拒绝,或者 无线电信号不可用.

int STATE_OUT_OF_SERVICE = Phone is not registered with any operator, the phone can be currently searching a new operator to register to, or not searching to registration at all, or registration is denied, or radio signal is not available.

int STATE_POWER_OFF =电话无线电已明确关闭.

int STATE_POWER_OFF = Radio of telephony is explicitly powered off.

这篇关于在网络单元更改时未调用ACTION_PHONE_STATE_CHANGED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆