【问题标题】:Detecting incoming call [duplicate]检测来电[重复]
【发布时间】:2016-11-10 10:46:06
【问题描述】:

在这里,我正在创建一个应用程序,以便在收到短信和收到来电时启动 flash。闪光灯正在接收短信,但不能通话,为什么?任何人都可以帮忙吗?很多天以来,我一直坚持这一点。在此先感谢,我的代码如下。 Flash 只在接收短信时执行,而不是在接听电话时执行。期待您的指导。当我对此进行搜索时,我只得到了创建自己的应用程序的类和方法。要求解释

public class SMSReceiver extends BroadcastReceiver {
        Boolean call;
        private Camera camera;
        int count;
        long delaytime;
        Editor editor;
        String flashtype;
        private boolean hasFlash;
        private boolean isFlashOn;
        private boolean isFlashblinking;
        private boolean mActive;
        private Handler mHander;
        private final Runnable mRunnable;
        private boolean mSwap;
        private Context mcontext;
        AudioManager myAudioManager;
        String noofblinks;
        int numbofblink;
        Parameters params;
        SharedPreferences pref;
        StringBuilder result;
        Boolean ring;
        Boolean silent;
        Boolean sms;
        String timetoblink;
        Boolean vibrate;

        public class PhoneListener extends PhoneStateListener {
            private Context context;

            public PhoneListener(Context c) {
                Log.i("CallRecorder", "PhoneListener constructor");
                this.context = c;
            }

            public void onCallStateChanged(int state, String incomingNumber) {
                switch (state) {
                case 0:
                    try {
                        SMSReceiver.this.mHander
                                .removeCallbacks(SMSReceiver.this.mRunnable);
                        if (SMSReceiver.this.camera != null) {
                            SMSReceiver.this.camera.release();
                        }
                    } catch (Exception e) {
                        try {
                            SMSReceiver.this.camera.release();
                        } catch (Exception e2) {
                        }
                    }
                case 1:
                    try {
                        if (SMSReceiver.this.call.booleanValue()) {
                            if (SMSReceiver.this.myAudioManager.getRingerMode() == 0
                                    && SMSReceiver.this.silent.booleanValue()) {
                                SMSReceiver.this.flash();
                            }
                            if (SMSReceiver.this.myAudioManager.getRingerMode() == 1
                                    && SMSReceiver.this.vibrate.booleanValue()) {
                                SMSReceiver.this.flash();
                            }
                            if (SMSReceiver.this.myAudioManager.getRingerMode() == 2
                                    && SMSReceiver.this.ring.booleanValue()) {
                                SMSReceiver.this.flash();
                            }
                        }
                    } catch (Exception e3) {
                    }
                case 2:
                    try {
                        SMSReceiver.this.mHander
                                .removeCallbacks(SMSReceiver.this.mRunnable);
                        if (SMSReceiver.this.camera != null) {
                            SMSReceiver.this.camera.release();
                        }
                    } catch (Exception e4) {
                        try {
                            SMSReceiver.this.camera.release();
                        } catch (Exception e5) {
                        }
                    }
                default:
                }
            }
        }

        public SMSReceiver() {
            this.mHander = new Handler();
            this.mActive = false;
            this.mSwap = true;
            this.isFlashblinking = true;
            this.count = 0;
            this.mRunnable = new Runnable() {

                @Override
                    // TODO Auto-generated method stub
                    public void run() {
                        try {
                            SMSReceiver sMSReceiver = SMSReceiver.this;
                            sMSReceiver.count++;
                        } catch (Exception e) {
                        }
                        if (SMSReceiver.this.mActive) {
                            if (SMSReceiver.this.count >= SMSReceiver.this.numbofblink * 2) {
                                try {
                                    SMSReceiver.this.mHander
                                            .removeCallbacks(SMSReceiver.this.mRunnable);
                                    SMSReceiver.this.camera.release();
                                } catch (Exception e2) {
                                }
                            }
                            if (SMSReceiver.this.isFlashOn) {
                                SMSReceiver.this.turnOffFlash();
                            } else {
                                SMSReceiver.this.turnOnFlash();
                            }
                            try {
                                SMSReceiver.this.mHander.postDelayed(
                                        SMSReceiver.this.mRunnable,
                                        SMSReceiver.this.delaytime);
                            } catch (Exception e3) {
                            }
                        }
                    }
            };
        }

        public void onReceive(Context context, Intent intent) {
            try {
                this.mcontext = context;
                this.count = 0;
                try {
                    this.pref = PreferenceManager
                            .getDefaultSharedPreferences(this.mcontext);
                    this.editor = this.pref.edit();
                    this.call = Boolean.valueOf(this.pref.getBoolean("call", true));
                    this.sms = Boolean.valueOf(this.pref.getBoolean("sms", true));
                    this.timetoblink = this.pref.getString("blinktime", "200");
                    this.noofblinks = this.pref.getString("noofblinks", "5");
                    this.ring = Boolean.valueOf(this.pref.getBoolean("ring", true));
                    this.vibrate = Boolean.valueOf(this.pref.getBoolean("vibrate",
                            true));
                    this.silent = Boolean.valueOf(this.pref.getBoolean("silent",
                            true));
                    this.flashtype = this.pref.getString("flashtype", "1");
                    this.delaytime = Long.parseLong(this.timetoblink);
                    this.numbofblink = Integer.parseInt(this.noofblinks);
                    this.myAudioManager = (AudioManager) this.mcontext
                            .getSystemService("audio");
                } catch (Exception e) {
                }
                ((TelephonyManager) this.mcontext.getSystemService("phone"))
                        .listen(new PhoneListener(context), 32);
            } catch (Exception e2) {
            }
        }

        public void flash() {
            try {
                this.hasFlash = this.mcontext.getPackageManager().hasSystemFeature(
                        "android.hardware.camera.flash");
                if (this.hasFlash) {
                    getCamera();
                    startStrobe();
                    return;
                }
                AlertDialog alert = new Builder(this.mcontext).create();
                alert.setTitle("Error");
                alert.setMessage("Sorry, your device doesn't support flash light!");

                alert.setButton("OK", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                });
                alert.show();
            } catch (Exception e) {
            }
        }

        private void getCamera() {
            if (this.camera == null) {
                try {
                    this.camera = Camera.open();
                    this.params = this.camera.getParameters();
                } catch (Exception e) {
                    Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
                }
            }
        }

        private void turnOnFlash() {
            try {
                if (!this.isFlashOn && this.camera != null && this.params != null) {
                    this.params = this.camera.getParameters();
                    if (this.flashtype.equals("2")) {
                        this.params.setFlashMode("torch");
                    } else if (this.flashtype.equals("3")) {
                        this.params.setFlashMode("torch");
                    } else {
                        this.params.setFlashMode("torch");
                    }
                    this.camera.setParameters(this.params);
                    this.camera.startPreview();
                    this.isFlashOn = true;
                }
            } catch (Exception e) {
            }
        }

        private void turnOffFlash() {
            try {
                if (this.isFlashOn && this.camera != null && this.params != null) {
                    this.params = this.camera.getParameters();
                    this.params.setFlashMode("off");
                    this.camera.setParameters(this.params);
                    this.camera.stopPreview();
                    this.isFlashOn = false;
                }
            } catch (Exception e) {
            }
        }

        private void startStrobe() {
            try {
                this.mActive = true;
                this.mHander.post(this.mRunnable);
            } catch (Exception e) {
            }
        }
    }

【问题讨论】:

    标签: android sms phone-call incoming-call


    【解决方案1】:

    【讨论】:

    • 你检查我的代码了吗?我想对我的代码进行更改.. 你能跟踪我的代码中没有检测到调用 flash 的错误吗
    • 是的,让我试试,但如果找到解决方案,请试试这个
    • 当然,会帮助你尝试:)
    【解决方案2】:

    制作广播接收器以了解来电

    在 AndroidManifes.xml 中编写该代码

    <receiver android:name=".ServiceReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
    

    然后创建一个这样的新类。

    public class ServiceReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(final Context context, Intent intent) {
            TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            telephony.listen(new PhoneStateListener(){
                @Override
                public void onCallStateChanged(int state, String incomingNumber)            {
           if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
        // Run the flash in that line
            }
    
                }
            },PhoneStateListener.LISTEN_CALL_STATE);
        }
    }
    

    【讨论】:

    • 并且还使用了androidManifest.xml中Read.phone状态的权限
    • 不能在原始类型 int 上调用 equals(String)
    • 抱歉,我在没有任何 IDE 的情况下编写了该代码。
    • 写那行代码 if(state==TelephonyManager.EXTRA_STATE_RINGING){ }
    • 它不会在此代码上运行 flash
    猜你喜欢
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 2014-06-09
    • 1970-01-01
    • 2014-07-26
    相关资源
    最近更新 更多