Blog

  • Flashlight Alert on SMS Android

    I'm not going to share the full fledged code of the Flashlight Alert app but I will share the code behind the main logic i.e.of Flashlight, Flashlight blink and register BroadcastReceiver for Sms & Incoming Calls dynamically.

    Flashlight: As the Camera API deprecated in API level 21, it is recommended to use Camere2 API for new Applications. Camera2 does not provide back-support to API level below 21, so I had to write separate code for API level below 21 and above.

    if (sdkVersion < Build.VERSION_CODES.M) {
    
                        //code for Camera API- 1-20
    
                        if (isChecked) {   //if the ToggleButton is checked
    
                            camera = Camera.open();
                            Camera.Parameters parameters = camera.getParameters();
                            parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                            camera.setParameters(parameters);
                            camera.startPreview();and upward
                        }
                        else {
                            camera.stopPreview();
                            camera.release();
                        }
    
                    } else{
    
                        //code for Camera2 API- 21 or higher
    
                        mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
                        try {
                            mCameraId = mCameraManager.getCameraIdList()[0];
                            if(isChecked){
                                mCameraManager.setTorchMode(mCameraId, true);
                            }else{
                                mCameraManager.setTorchMode(mCameraId, false);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
    
                    }
    }
    

    Flashlight blink: There are many ways to do this but I find this way more suitable than others for my need. Below code works perfectly on API Level upto 21[Tested]. The same can be easily done using Camera2 for API level 21 and above.

    String string= "01010101010101010101"; //flashlight will blink 10 times
                                long longDelay= 50;
                                for(int i=0; i<string.length();i++){
                                    if(string.charAt(i)== '0'){
                                        camera = Camera.open();
                                        Camera.Parameters parameters = camera.getParameters();
                                        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                                        camera.setParameters(parameters);
                                        camera.startPreview();
                                    }else{
                                        camera.stopPreview();
                                        camera.release();
                                    }
                                    try{
                                        Thread.sleep(longDelay);
                                    }catch (Exception e){
                                        Log.i("exceptionException", "This is an blinking exception");
                                        e.printStackTrace();
                                    }
                                }
    

    Broadcast Receiver for Call/Sms: Here I have implemented the Broadcast Receiver dynamically rather than creating a class and extending it to BroadcastReceiver and mentioning the class name in the AndroidManifest.xml.

    private BroadcastReceiver broadcastReceiverSms, broadcastReceiverCall;
    
    //Sms Intent Filter
    IntentFilter intentFilter= new IntentFilter();
    intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
    
    //Call Intent Filter
    IntentFilter intentFilter= new IntentFilter();
    intentFilter.addAction("android.intent.action.PHONE_STATE");
    
    //broadcast receiver 
    broadcastReceiverCall= new BroadcastReceiver() {
                            @Override
                            public void onReceive(Context context, Intent intent) {
                                
    			//flashlight blink logic for call & sms
    
                            }
                        };
    
    //start the broadcast receiver
    registerReceiver(broadcastReceiverCall, intentFilter); //same can be done for sms
    
    //stop the broadcast receiver
    unregisterReceiver(broadcastReceiverCall);
    

    At last don't forget to add the permissions for Sms and Call in the AndroidManifest.xml file.

     

    Thanks.

Tags: android

View All PostsLeave a Comment
comments powered by Disqus

Mobile Applications

Video Content

Bigdata & NoSQL

SaaS Applications

Miscellaneous

Archives


Alexa Certified Site Stats for www.oodlestechnologies.com