sumber : http://agusharyanto.net/wordpress/?p=1462
Sekarang mari kita mulai langkah-langkahnya
1. Buka Android Studio buat project baru dengan nama “Senter Flash Light”.
Klik Next terus sampai Finish
2. Kita butuh gambar untuk tombol on, tombol off dan bola lampu on, bola lampu off. Kalian bisa cari di internet untuk gambar-gambarnya.
Gambar |
Nama file |
Tombol on |
button_on.png |
Tombol off |
button_off.png |
Bola lampu on |
light_on.png |
Bola lampu off |
light_off.png |
Pastekan file-file gambar diatas ke direktory res→drawable
3. kita juga perlu suara ketika tombol on atau tombol off disentuh. Bisa dicari juga diinternet yah. Mahasiswa pasti bisa… semangat..
4. pada directory res buat directory baru dengan nama raw pastekan file sound_on_off.mp3 ke directory raw.
5. Agar bisa mengakses atau menggunakan lampu flash light camera, kita perlu menambahkan permission di AndroidManifest.xmltambahkan permission berikut ini.
1 |
< uses-permission android:name = "android.permission.CAMERA" /> |
2 |
< uses-permission android:name = "android.permission.FLASHLIGHT" /> |
3 |
< uses-feature android:name = "android.hardware.camera" /> |
4 |
< uses-feature android:name = "android.hardware.camera.flash" /> |
6. Sekarang kita kebagian user interfacenya, untuk itu pada directory res→layout edit activity_main.xml lalu ketikan kode berikut.
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
04 |
android:id = "@+id/activity_main" |
05 |
android:layout_width = "match_parent" |
06 |
android:layout_height = "match_parent" |
07 |
android:paddingBottom = "@dimen/activity_vertical_margin" |
08 |
android:paddingLeft = "@dimen/activity_horizontal_margin" |
09 |
android:paddingRight = "@dimen/activity_horizontal_margin" |
10 |
android:paddingTop = "@dimen/activity_vertical_margin" |
11 |
android:background = "#fff" |
12 |
tools:context = "tutorialanadroid.agusharyanto.net.senterflashlight.MainActivity" > |
15 |
android:id = "@+id/imageViewLight" |
16 |
android:layout_width = "256dp" |
17 |
android:layout_height = "256dp" |
18 |
android:layout_centerHorizontal = "true" |
19 |
android:src = "@drawable/light_off" /> |
22 |
android:id = "@+id/imageButton_on_off" |
23 |
android:layout_width = "wrap_content" |
24 |
android:layout_height = "wrap_content" |
25 |
android:layout_below = "@+id/imageViewLight" |
26 |
android:layout_centerHorizontal = "true" |
27 |
android:layout_marginTop = "50dp" |
28 |
android:background = "#fff" |
29 |
android:src = "@drawable/button_on" /> |
7. sekarang mari kita ke otak aplikasinya, edit MainActivity.java lalu ketikan kode berikut.
001 |
package tutorialanadroid.agusharyanto.net.senterflashlight; |
003 |
import android.content.Context; |
004 |
import android.content.DialogInterface; |
005 |
import android.content.pm.PackageManager; |
006 |
import android.hardware.camera2.CameraManager; |
007 |
import android.media.MediaPlayer; |
008 |
import android.os.Build; |
009 |
import android.os.Bundle; |
010 |
import android.support.v7.app.AlertDialog; |
011 |
import android.support.v7.app.AppCompatActivity; |
012 |
import android.view.View; |
013 |
import android.widget.ImageButton; |
014 |
import android.widget.ImageView; |
016 |
public class MainActivity extends AppCompatActivity { |
018 |
private CameraManager cameraManager; |
019 |
private String cameraId; |
020 |
private ImageButton imangeButtonOnOff; |
021 |
private ImageView imageViewLight; |
022 |
private Boolean isTorchOn; |
023 |
private MediaPlayer mp; |
026 |
protected void onCreate(Bundle savedInstanceState) { |
027 |
super .onCreate(savedInstanceState); |
028 |
setContentView(R.layout.activity_main); |
029 |
imangeButtonOnOff = (ImageButton) findViewById(R.id.imageButton_on_off); |
030 |
imageViewLight = (ImageView) findViewById(R.id.imageViewLight); |
032 |
Boolean isFlashAvailable = getApplicationContext().getPackageManager() |
033 |
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); |
035 |
if (!isFlashAvailable) { |
037 |
AlertDialog alert = new AlertDialog.Builder(MainActivity. this ) |
039 |
alert.setTitle( "Error !!" ); |
040 |
alert.setMessage( "Device anda tidak mendukung flash light!" ); |
041 |
alert.setButton(DialogInterface.BUTTON_POSITIVE, "OK" , new DialogInterface.OnClickListener() { |
042 |
public void onClick(DialogInterface dialog, int which) { |
052 |
cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); |
053 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
056 |
cameraId = cameraManager.getCameraIdList()[ 0 ]; |
063 |
imangeButtonOnOff.setOnClickListener( new View.OnClickListener() { |
065 |
public void onClick(View v) { |
074 |
} catch (Exception e) { |
080 |
public void turnOnFlashLight() { |
083 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
084 |
cameraManager.setTorchMode(cameraId, true ); |
086 |
imangeButtonOnOff.setImageResource(R.drawable.button_off); |
087 |
imageViewLight.setImageResource(R.drawable.light_on); |
089 |
} catch (Exception e) { |
094 |
public void turnOffFlashLight() { |
096 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
097 |
cameraManager.setTorchMode(cameraId, false ); |
099 |
imangeButtonOnOff.setImageResource(R.drawable.button_on); |
100 |
imageViewLight.setImageResource(R.drawable.light_off); |
103 |
} catch (Exception e) { |
108 |
private void playOnOffSound(){ |
110 |
mp = MediaPlayer.create(MainActivity. this , R.raw.sound_on_off); |
111 |
mp.setOnCompletionListener( new MediaPlayer.OnCompletionListener() { |
114 |
public void onCompletion(MediaPlayer mp) { |
122 |
protected void onStop() { |
130 |
protected void onPause() { |
138 |
protected void onResume() { |
8. Kalau kita lihat kode diatas belum menerapkan self permission jadi agar dapat run dengan baik di Android versi Marshmallow keatas kita perlu merubah targetSdkVersion nya menjadi 21 pada file build.gradle (module:app)
9. Setelah semua selesai mari kita run projectnya. Sentuh tombol hijau untuk menyalakan lampu Flash Light.
Untuk pdfnya bisa lihat disini
Semoga bermanfaat
Salam Hangat Developer Android
Komentar Terbaru