Advertisement
sowmyar

Android Flashlight

Jul 30th, 2014
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. public class Flashlight extends Activity {
  2.  
  3.     private static Camera cam = null;
  4.     private Camera.Parameters par;
  5.  
  6.     @Override
  7.     protected void onCreate(Bundle savedInstanceState) {
  8.         super.onCreate(savedInstanceState);
  9.         setContentView(R.layout.activity_flashlight);
  10.         Context context = this;
  11.         PackageManager packageManager = context.getPackageManager();
  12.         blink();
  13.        }
  14.  
  15.      @Override
  16.     public boolean onCreateOptionsMenu(Menu menu) {
  17.         // Inflate the menu; this adds items to the action bar if it is present.
  18.         getMenuInflater().inflate(R.menu.flashlight, menu);
  19.         return true;
  20.     }
  21.  
  22.     @Override
  23.     public boolean onOptionsItemSelected(MenuItem item) {
  24.         // Handle action bar item clicks here. The action bar will
  25.         // automatically handle clicks on the Home/Up button, so long
  26.         // as you specify a parent activity in AndroidManifest.xml.
  27.         int id = item.getItemId();
  28.         if (id == R.id.action_settings) {
  29.             return true;
  30.         }
  31.         return super.onOptionsItemSelected(item);
  32.     }
  33.  
  34.  
  35.     public void blink() {
  36.             for (int i = 0; i < 10; i++) {
  37.                 cameraon();
  38.                 cameraoff();
  39.        }
  40.     }
  41.  
  42.  
  43.     public void cameraon() {
  44.  
  45.         if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
  46.             cam = Camera.open();
  47.             try {
  48.  
  49.                 cam.setPreviewTexture(new SurfaceTexture(0));
  50.  
  51.             } catch (IOException e) {
  52.                 e.printStackTrace();
  53.             }
  54.             par = cam.getParameters();
  55.             par.setFlashMode(par.FLASH_MODE_ON);
  56.             par.setFlashMode(par.FLASH_MODE_TORCH);
  57.             cam.setParameters(par);
  58.             cam.startPreview();
  59.         }
  60.     }
  61.  
  62.      public void cameraoff(){
  63.  
  64.         par.setFlashMode(par.FLASH_MODE_OFF);
  65.         cam.setParameters(par);
  66.         cam.stopPreview();
  67.         cam.release();
  68.         cam = null;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement