【问题标题】:PhoneStateListener and BroadcastReceiverPhoneStateListener 和 BroadcastReceiver
【发布时间】:2013-01-24 14:16:54
【问题描述】:

我创建了一个 BroadcastReceiver 来监听他的电话状态并采取相应的行动。

这里的问题是它似乎没有从我的数据库中获取正确的值,并且从一开始处于空闲状态的 toast 显示两次,第二次调用后显示四次等等......

我猜我的班级出了点问题,但我不知道是什么...

这是一个代码sn-p:

public class PhoneListener extends BroadcastReceiver{

    private DBAdapter   db;
    private Cursor      data;
    private Cursor      options;
    private int     activer;
    private  int        mouvement;
    private int     verticalite;

    @Override
    public void onReceive(Context context, Intent intent) {

        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);

    }

    public void stopService(Context context){
        Intent alerte = new Intent(context, SensorOrientation.class);
        alerte.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.stopService(alerte);
    }

    public void startService(Context context){
        Intent alerte = new Intent(context, SensorOrientation.class);
        alerte.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startService(alerte);
    }

    public void startnotif(Context context){
        Intent intent   =   new Intent(context, SecuriteHauteReboot.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startService(intent);   
    }

    public class CustomPhoneStateListener extends PhoneStateListener {

        //private static final String TAG = "PhoneStateChanged";
        Context context; //Context to make Toast if required 
        public CustomPhoneStateListener(Context context) {
            super();
            this.context = context;
        }

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);

            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                db          =   new DBAdapter(context);
                data        =   db.selectWidget();
                options     =   db.selectVerticalite();
                activer     =   data.getInt(data.getColumnIndex("activer"));
                mouvement   =   options.getInt(options.getColumnIndex("activer_alarme_mouvement"));
                verticalite =   options.getInt(options.getColumnIndex("activer_alarme_verticalite"));
                data.close();
                options.close();
                //when Idle i.e no call
                Toast.makeText(context,
                        String.format(" "+mouvement+" "+activer),
                        Toast.LENGTH_SHORT).show();
                if(activer == 1){
                    if(mouvement == 1 || verticalite == 1){
                        Toast.makeText(context,
                                String.format("and I'm here...."),
                                Toast.LENGTH_SHORT).show();
                        startService(context);
                        startnotif(context);
                    }
                }
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                //when Off hook i.e in call
                //Make intent and start your service here
                if (activer == 1){
                    if(mouvement == 1 || verticalite == 1){
                        stopService(context);
                    }
                }
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                //when Ringing
                if (activer == 1){
                    if(mouvement == 1 || verticalite == 1){
                        Toast.makeText(context,
                                String.format("and I'm here...."),
                                Toast.LENGTH_SHORT).show();
                        stopService(context);
                    }
                }
                break;
            default:
                break;
            }
        }
    }
}  

【问题讨论】:

    标签: android broadcastreceiver phone-state-listener


    【解决方案1】:

    您可能在 onCallStateChanged(数据库 I/O 操作)中做了很多事情。我在这里遇到了与BroadcastReceiver 类似的问题:Changing ringer volume during ringing。对于测试,请尝试对 BroadcastReceiver 中的静态字段(而不是 LiteSQL)进行操作并检查它是否有帮助。

    【讨论】:

    • 感谢灵魂掠夺者!这正是问题所在。我不喜欢使用太多静态值,所以我只是创建了服务,它就像一个魅力!问题解决了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    相关资源
    最近更新 更多