you can check :
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="com.google.android.previders.gsf.permission.READ_GSERVICES" />
<uses-feature android:name="android.hardware.camera"/>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#333"
tools:context=".MainActivity">
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="40dp"
android:text="Wow Kya Bat.....Shashi !!"
android:textColor="#DF8BB1"
android:textSize="25sp"
android:textStyle="italic" />
<ImageView
android:id="@+id/Switch_btn"
android:layout_width="179dp"
android:layout_height="206dp"
android:layout_below="@+id/txt"
android:layout_marginStart="112dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="68dp"
android:layout_marginEnd="120dp"
android:layout_marginRight="20dp"
android:src="@drawable/mytorch" />
</RelativeLayout>
package com.example.myflashlightapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import static androidx.core.os.LocaleListCompat.create;
public class MainActivity extends AppCompatActivity {
private ImageView flashLight;
private static final int CAMERA_REQUEST=50;
private boolean flashLightStatus=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
flashLight=findViewById( R.id.Switch_btn );
flashLight.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
{
if (flashLightStatus)
turnLightOff();
else
turnLightOn();
}
}
private void turnLightOff() {
CameraManager cameraManager=(CameraManager)getSystemService( Context.CAMERA_SERVICE );
try{
String cam_ID=cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode( cam_ID,false );
flashLightStatus=false;
}
catch (CameraAccessException e){
}
}
private void turnLightOn() {
CameraManager cameraManager=(CameraManager)getSystemService( Context.CAMERA_SERVICE );
try{
String cam_ID=cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode( cam_ID,true );
flashLightStatus=true;
}
catch (CameraAccessException e){
}
}
} );
ActivityCompat.requestPermissions( MainActivity.this,new String[]{Manifest.permission.CAMERA},CAMERA_REQUEST );
final boolean hasCameraFlash=getPackageManager().hasSystemFeature( PackageManager.FEATURE_CAMERA_FLASH );
if (!hasCameraFlash) {
AlertDialog alert=new AlertDialog.Builder( MainActivity.this )
.create();
alert.setTitle( "Error !!" );
alert.setMessage( "Your device does't support flash light !!" );
alert.setButton( DialogInterface.BUTTON_POSITIVE,"Ok",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
finish();
System.exit( 0 );
}
} );
alert.show();
return;
}
}
@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String[] permissions,@NonNull int[] grantResults) {
{
switch (requestCode){
case CAMERA_REQUEST:
if(!(grantResults.length>0)&& !(grantResults[0]==PackageManager.PERMISSION_GRANTED))
{
AlertDialog alert=new AlertDialog.Builder( MainActivity.this )
.create();
alert.setTitle( "Error !!" );
alert.setMessage( "CMERA PERMISSION NOT GRANTED!" );
alert.setButton( DialogInterface.BUTTON_POSITIVE,"OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
finish();
System.exit( 0 );
}
} );
alert.show();
return;
}
break;
}
}
}
}
minSdkVersion 16targetSdkVersion 26
Manifest.xml
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="com.google.android.previders.gsf.permission.READ_GSERVICES" />
<uses-feature android:name="android.hardware.camera"/>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#333"
tools:context=".MainActivity">
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="40dp"
android:text="Wow Kya Bat.....Shashi !!"
android:textColor="#DF8BB1"
android:textSize="25sp"
android:textStyle="italic" />
<ImageView
android:id="@+id/Switch_btn"
android:layout_width="179dp"
android:layout_height="206dp"
android:layout_below="@+id/txt"
android:layout_marginStart="112dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="68dp"
android:layout_marginEnd="120dp"
android:layout_marginRight="20dp"
android:src="@drawable/mytorch" />
</RelativeLayout>
MainActivity.java
package com.example.myflashlightapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import static androidx.core.os.LocaleListCompat.create;
public class MainActivity extends AppCompatActivity {
private ImageView flashLight;
private static final int CAMERA_REQUEST=50;
private boolean flashLightStatus=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
flashLight=findViewById( R.id.Switch_btn );
flashLight.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
{
if (flashLightStatus)
turnLightOff();
else
turnLightOn();
}
}
private void turnLightOff() {
CameraManager cameraManager=(CameraManager)getSystemService( Context.CAMERA_SERVICE );
try{
String cam_ID=cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode( cam_ID,false );
flashLightStatus=false;
}
catch (CameraAccessException e){
}
}
private void turnLightOn() {
CameraManager cameraManager=(CameraManager)getSystemService( Context.CAMERA_SERVICE );
try{
String cam_ID=cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode( cam_ID,true );
flashLightStatus=true;
}
catch (CameraAccessException e){
}
}
} );
ActivityCompat.requestPermissions( MainActivity.this,new String[]{Manifest.permission.CAMERA},CAMERA_REQUEST );
final boolean hasCameraFlash=getPackageManager().hasSystemFeature( PackageManager.FEATURE_CAMERA_FLASH );
if (!hasCameraFlash) {
AlertDialog alert=new AlertDialog.Builder( MainActivity.this )
.create();
alert.setTitle( "Error !!" );
alert.setMessage( "Your device does't support flash light !!" );
alert.setButton( DialogInterface.BUTTON_POSITIVE,"Ok",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
finish();
System.exit( 0 );
}
} );
alert.show();
return;
}
}
@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String[] permissions,@NonNull int[] grantResults) {
{
switch (requestCode){
case CAMERA_REQUEST:
if(!(grantResults.length>0)&& !(grantResults[0]==PackageManager.PERMISSION_GRANTED))
{
AlertDialog alert=new AlertDialog.Builder( MainActivity.this )
.create();
alert.setTitle( "Error !!" );
alert.setMessage( "CMERA PERMISSION NOT GRANTED!" );
alert.setButton( DialogInterface.BUTTON_POSITIVE,"OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
finish();
System.exit( 0 );
}
} );
alert.show();
return;
}
break;
}
}
}
}
0 comments:
Post a Comment