Flashlight Altert on Sms and Call in Android
Posted By : Bhriguraj Salponia | 12-Jul-2016
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.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Bhriguraj Salponia
Bhriguraj is a bright mobile app developer and have good knowledge of C, Java, Android, Sql , Html. His hobbies are playing video games.