Monday 14 February 2022

Alarm Manager Example in Android

 



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">


    <Button

        android:id="@+id/btn_start_alarm"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerInParent="true"

        android:text="Start Alarm"

        android:textAllCaps="false"/>


</RelativeLayout>



MainActivity.java

import androidx.appcompat.app.AppCompatActivity;


import android.app.AlarmManager;

import android.app.PendingIntent;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;


public class MainActivity extends AppCompatActivity {


    private Button mBtnStart;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        mBtnStart = findViewById(R.id.btn_start_alarm);

        mBtnStart.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                startAlarm(MainActivity.this);

            }

        });

    }


    public static void startAlarm(Context context) {

        Intent intent = new Intent(context, MyAlarmReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(

                context.getApplicationContext(), 0 , intent, 0);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);

        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()

                + (10 * 1000), pendingIntent); //10 seconds interval

    }

}



MyAlarmReceiver.java

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.widget.Toast;


public class MyAlarmReceiver extends BroadcastReceiver {

    @Override

    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, "Alarm Started Successfully", Toast.LENGTH_SHORT).show();

    }

}  



AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.surabhiglobal.alarmexample">


    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"

        android:theme="@style/Theme.AlarmExample">

        <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>


        <receiver android:name=".MyAlarmReceiver" />


    </application>


</manifest>

Monday 7 February 2022

Foreground Service Example in Android

 


 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context=".MainActivity">


    <Button

        android:id="@+id/btn_start"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Start Service"

        android:layout_margin="20sp"/>


    <Button

        android:id="@+id/btn_stop"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Stop Service"

        android:layout_margin="20sp"/>


</LinearLayout>


MainActivity.java


import androidx.appcompat.app.AppCompatActivity;


import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;


public class MainActivity extends AppCompatActivity {

    private Button btn_start,btn_stop;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        btn_start = findViewById(R.id.btn_start);

        btn_stop = findViewById(R.id.btn_stop);


        btn_start.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                startService(new Intent(MainActivity.this,ForegroundService.class));

            }

        });

        btn_stop.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                stopService(new Intent(MainActivity.this,ForegroundService.class));

            }

        });

    }

}


ForegroundService.java


import android.app.NotificationChannel;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.app.Service;

import android.content.Intent;

import android.os.Binder;

import android.os.Build;

import android.os.IBinder;


import androidx.core.app.NotificationCompat;


public class ForegroundService extends Service {


    private final IBinder mBinder = new MyBinder();

    private static final String CHANNEL_ID = "2";


    @Override

    public IBinder onBind(Intent intent) {

        return mBinder;

    }


    @Override

    public int onStartCommand(Intent intent, int flags, int startId) {

        return START_STICKY;

    }


    @Override

    public void onCreate() {

        super.onCreate();

        buildNotification();

    }


    private void buildNotification() {

        String stop = "stop";

        PendingIntent broadcastIntent = PendingIntent.getBroadcast(

                this, 0, new Intent(stop), PendingIntent.FLAG_UPDATE_CURRENT);

        // Create the persistent notification

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)

                .setContentTitle(getString(R.string.app_name))

                .setContentText("Foreground service is working")

                .setOngoing(true)

                .setContentIntent(broadcastIntent);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, getString(R.string.app_name),

                    NotificationManager.IMPORTANCE_DEFAULT);

            channel.setShowBadge(false);

            channel.setDescription("Foreground service is working");

            channel.setSound(null, null);

            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            manager.createNotificationChannel(channel);

        }

        startForeground(1, builder.build());

    }

    public class MyBinder extends Binder {

        public ForegroundService getService() {

            return ForegroundService.this;

        }

    }

}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.surabhiglobal.foregroundserviceexample">

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.ForegroundServiceExample">

        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".ForegroundService"
            android:enabled="true"
            android:exported="false"/>

    </application>

</manifest>

Background location tracking in android even if the app get closed

  


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context=".MainActivity">


    <Button

        android:id="@+id/btn_get"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Get Location"

        android:layout_margin="20sp"/>


</LinearLayout>


MainActivity.java

import androidx.appcompat.app.AppCompatActivity;


import android.Manifest;

import android.app.AlertDialog;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.content.IntentSender;

import android.location.LocationManager;

import android.net.Uri;

import android.os.Bundle;

import android.provider.Settings;

import android.view.View;

import android.widget.Button;


import com.google.android.gms.common.api.ResolvableApiException;

import com.google.android.gms.location.LocationRequest;

import com.google.android.gms.location.LocationServices;

import com.google.android.gms.location.LocationSettingsRequest;

import com.google.android.gms.location.LocationSettingsResponse;

import com.karumi.dexter.Dexter;

import com.karumi.dexter.MultiplePermissionsReport;

import com.karumi.dexter.PermissionToken;

import com.karumi.dexter.listener.PermissionRequest;

import com.karumi.dexter.listener.multi.MultiplePermissionsListener;

import java.util.List;


public class MainActivity extends AppCompatActivity {

    private Button btn_get;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);


        btn_get = findViewById(R.id.btn_get);

        btn_get.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

                    enableLocationSettings();

                } else {

                    requestAppPermissions();

                }

            }

        });

    }

    private void requestAppPermissions() {

        Dexter.withActivity(MainActivity.this)

                .withPermissions(

                        Manifest.permission.ACCESS_FINE_LOCATION,

                        Manifest.permission.ACCESS_COARSE_LOCATION)

                .withListener(new MultiplePermissionsListener() {

                    @Override

                    public void onPermissionsChecked(MultiplePermissionsReport report) {

                        // check if all permissions are granted

                        if (report.areAllPermissionsGranted()) {

                            // do you work now

                            //interact.downloadImage(array);

                            startService(new Intent(MainActivity.this, ForegroundService.class));

                        }


                        // check for permanent denial of any permission

                        if (report.isAnyPermissionPermanentlyDenied()) {

                            // permission is denied permenantly, navigate user to app settings

                            showSettingsDialog();

                            //finish();

                        }

                    }

                    @Override

                    public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {

                        token.continuePermissionRequest();

                    }

                })

                .onSameThread()

                .check();

    }

    private void showSettingsDialog() {

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

        builder.setTitle("Need Permissions");

        builder.setMessage("This app needs permission to use this feature. You can grant them in app settings.");

        builder.setPositiveButton("GOTO SETTINGS", new DialogInterface.OnClickListener() {

            @Override

            public void onClick(DialogInterface dialog, int which) {

                dialog.cancel();

                openSettings();

            }

        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            @Override

            public void onClick(DialogInterface dialog, int which) {

                dialog.cancel();

            }

        });

        builder.show();

    }

    private void openSettings() {

        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);

        Uri uri = Uri.fromParts("package", getPackageName(), null);

        intent.setData(uri);

        startActivityForResult(intent, 101);

    }

    protected void enableLocationSettings() {

        LocationRequest locationRequest = LocationRequest.create()

                .setInterval(1000)

                .setFastestInterval(3000)

                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()

                .addLocationRequest(locationRequest);

        LocationServices

                .getSettingsClient(this)

                .checkLocationSettings(builder.build())

                .addOnSuccessListener(this, (LocationSettingsResponse response) -> {

                    // startUpdatingLocation(...);

                })

                .addOnFailureListener(this, ex -> {

                    if (ex instanceof ResolvableApiException) {

                        // Location settings are NOT satisfied,  but this can be fixed  by showing the user a dialog.

                        try {

                            // Show the dialog by calling startResolutionForResult(),  and check the result in onActivityResult().

                            ResolvableApiException resolvable = (ResolvableApiException) ex;

                            resolvable.startResolutionForResult(this, 123);

                        } catch (IntentSender.SendIntentException sendEx) {

                            // Ignore the error.

                        }

                    }

                });

    }

}


ForegroundService.java


import android.Manifest;

import android.app.NotificationChannel;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.app.Service;

import android.content.Intent;

import android.content.pm.PackageManager;

import android.os.Binder;

import android.os.Build;

import android.os.IBinder;

import android.widget.Toast;


import androidx.core.app.NotificationCompat;

import androidx.core.content.ContextCompat;


import com.google.android.gms.location.FusedLocationProviderClient;

import com.google.android.gms.location.LocationCallback;

import com.google.android.gms.location.LocationRequest;

import com.google.android.gms.location.LocationResult;

import com.google.android.gms.location.LocationServices;


public class ForegroundService extends Service {


    private final IBinder mBinder = new MyBinder();

    private static final String CHANNEL_ID = "2";


    @Override

    public IBinder onBind(Intent intent) {

        return mBinder;

    }


    @Override

    public int onStartCommand(Intent intent, int flags, int startId) {

        return START_STICKY;

    }


    @Override

    public void onCreate() {

        super.onCreate();

        buildNotification();

        requestLocationUpdates();

    }


    private void buildNotification() {

        String stop = "stop";

        PendingIntent broadcastIntent = PendingIntent.getBroadcast(

                this, 0, new Intent(stop), PendingIntent.FLAG_UPDATE_CURRENT);

        // Create the persistent notification

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)

                .setContentTitle(getString(R.string.app_name))

                .setContentText("Location tracking is working")

                .setOngoing(true)

                .setContentIntent(broadcastIntent);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, getString(R.string.app_name),

                    NotificationManager.IMPORTANCE_DEFAULT);

            channel.setShowBadge(false);

            channel.setDescription("Location tracking is working");

            channel.setSound(null, null);

            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            manager.createNotificationChannel(channel);

        }


        startForeground(1, builder.build());

    }

    private void requestLocationUpdates() {

        LocationRequest request = new LocationRequest();

        request.setInterval(1000);

        request.setFastestInterval(3000);

        request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        FusedLocationProviderClient client = LocationServices.getFusedLocationProviderClient(this);

        int permission = ContextCompat.checkSelfPermission(this,

                Manifest.permission.ACCESS_FINE_LOCATION);

        if (permission == PackageManager.PERMISSION_GRANTED) {

            // Request location updates and when an update is

            // received, store the location in Firebase

            client.requestLocationUpdates(request, new LocationCallback() {

                @Override

                public void onLocationResult(LocationResult locationResult) {


                    String location = "Latitude : " + locationResult.getLastLocation().getLatitude() +

                            "\nLongitude : " + locationResult.getLastLocation().getLongitude();


                    Toast.makeText(ForegroundService.this, location, Toast.LENGTH_SHORT).show();

                }

            }, null);

        } else {

            stopSelf();

        }

    }

    public class MyBinder extends Binder {

        public ForegroundService getService() {

            return ForegroundService.this;

        }

    }

}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.surabhiglobal.foregroundserviceexample">


    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />


    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"

        android:theme="@style/Theme.ForegroundServiceExample">


        <activity

            android:name=".MainActivity"

            android:configChanges="orientation|screenSize">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <service

            android:name=".ForegroundService"

            android:enabled="true"

            android:exported="false"/>

    </application>

</manifest>


build.gradle (app level file)

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.surabhiglobal.foregroundserviceexample"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    //add this two dependencies
    implementation 'com.google.android.gms:play-services-location:18.0.0'
    implementation 'com.karumi:dexter:4.2.0'

}

Capture photos in android without opening camera

 



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">


    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal"

        android:weightSum="2"

        android:layout_margin="10dp">


        <Button

            android:id="@+id/btn_front"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="Front Camera"

            android:layout_marginEnd="10dp" />


        <Button

            android:id="@+id/btn_back"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="Back Camera" />

    </LinearLayout>


    <ImageView

        android:id="@+id/iv_image"

        android:layout_width="250dp"

        android:layout_height="250dp"

        android:layout_centerInParent="true"/>


</RelativeLayout>


MainActivity.java

package com.surabhiglobal.example;


import android.Manifest;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.SurfaceTexture;

import android.hardware.Camera;

import android.net.Uri;

import android.os.Bundle;

import android.provider.Settings;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;


import androidx.appcompat.app.AppCompatActivity;


import com.karumi.dexter.Dexter;

import com.karumi.dexter.MultiplePermissionsReport;

import com.karumi.dexter.PermissionToken;

import com.karumi.dexter.listener.PermissionRequest;

import com.karumi.dexter.listener.multi.MultiplePermissionsListener;


import java.util.List;


public class MainActivity extends AppCompatActivity {


    private Button btn_front, btn_back;

    private String TAG = MainActivity.class.getName();

    private ImageView iv_image;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        btn_front = findViewById(R.id.btn_front);

        btn_back = findViewById(R.id.btn_back);

        iv_image = findViewById(R.id.iv_image);


        btn_front.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                requestAppPermissions("1");

            }

        });


        btn_back.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                requestAppPermissions("0");

            }

        });

    }


    private void requestAppPermissions(String type) {


        Dexter.withActivity(MainActivity.this)

                .withPermissions(

                        Manifest.permission.CAMERA)

                .withListener(new MultiplePermissionsListener() {

                    @Override

                    public void onPermissionsChecked(MultiplePermissionsReport report) {

                        if (report.areAllPermissionsGranted()) {

                            if (type.equals("0")) {

                                CaptureBackPhoto();

                            } else {

                                CaptureFrontPhoto();

                            }

                        }

                        // check for permanent denial of any permission

                        if (report.isAnyPermissionPermanentlyDenied()) {

                            // permission is denied permenantly, navigate user to app settings

                            showSettingsDialog();

                        }

                    }


                    @Override

                    public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {

                        token.continuePermissionRequest();

                    }


                })

                .onSameThread()

                .check();



    }


    private void showSettingsDialog() {

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

        builder.setTitle("Need Permissions");

        builder.setMessage("This app needs permission to use this feature. You can grant them in app settings.");

        builder.setPositiveButton("GOTO SETTINGS", new DialogInterface.OnClickListener() {

            @Override

            public void onClick(DialogInterface dialog, int which) {

                dialog.cancel();

                openSettings();

            }

        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            @Override

            public void onClick(DialogInterface dialog, int which) {

                dialog.cancel();

            }

        });

        builder.show();


    }


    private void openSettings() {

        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);

        Uri uri = Uri.fromParts("package", getPackageName(), null);

        intent.setData(uri);

        startActivityForResult(intent, 101);

    }



    private void CaptureFrontPhoto() {


        Log.d(TAG, "Preparing to take photo");

        Camera camera = null;


        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();


        int frontCamera = 1;

        //int backCamera=0;


        Camera.getCameraInfo(frontCamera, cameraInfo);


        try {

            camera = Camera.open(frontCamera);

            camera.enableShutterSound(false);

        } catch (RuntimeException e) {

            Log.d(TAG, "Camera not available: " + 1);

            camera = null;

            //e.printStackTrace();

        }

        try {

            if (null == camera) {

                Log.d(TAG, "Could not get camera instance");

            } else {

                Log.d(TAG, "Got the camera, creating the dummy surface texture");

                try {

                    camera.setPreviewTexture(new SurfaceTexture(0));

                    camera.startPreview();

                } catch (Exception e) {

                    Log.d(TAG, "Could not set the surface preview texture");

                    e.printStackTrace();

                }

                camera.takePicture(null, null, new Camera.PictureCallback() {


                    @Override

                    public void onPictureTaken(byte[] data, Camera camera) {


                        try {

                            Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);

                            iv_image.setImageBitmap(bmp);


                        } catch (Exception e) {

                            System.out.println(e.getMessage());

                        }


                        camera.release();

                    }

                });

            }


        } catch (Exception e) {

            camera.release();

        }

    }


    private void CaptureBackPhoto() {


        Log.d(TAG, "Preparing to take photo");

        Camera camera = null;


        try {

            camera = Camera.open();

            camera.enableShutterSound(false);

        } catch (RuntimeException e) {

            Log.d(TAG, "Camera not available: " + 1);

            camera = null;

            //e.printStackTrace();

        }

        try {

            if (null == camera) {

                Log.d(TAG, "Could not get camera instance");

            } else {

                Log.d(TAG, "Got the camera, creating the dummy surface texture");

                try {

                    camera.setPreviewTexture(new SurfaceTexture(0));

                    camera.startPreview();

                } catch (Exception e) {

                    Log.d(TAG, "Could not set the surface preview texture");

                    e.printStackTrace();

                }

                camera.takePicture(null, null, new Camera.PictureCallback() {


                    @Override

                    public void onPictureTaken(byte[] data, Camera camera) {


                        try {

                            Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);


                            iv_image.setImageBitmap(bmp);


                        } catch (Exception e) {

                            System.out.println(e.getMessage());

                        }


                        camera.release();

                    }

                });

            }


        } catch (Exception e) {

            camera.release();

        }

    }

}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.surabhiglobal.example">


    <uses-permission android:name="android.permission.CAMERA"/>

    <uses-feature android:name="android.hardware.camera" />


    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"

        android:theme="@style/Theme.ForegroundServiceExample">


        <activity

            android:name=".MainActivity"

            android:configChanges="orientation|screenSize">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

</manifest>


build.gradle (app level file)

plugins {

    id 'com.android.application'

}

android {

    compileSdkVersion 30

    buildToolsVersion "30.0.3"


    defaultConfig {

        applicationId "com.surabhiglobal.example"

        minSdkVersion 17

        targetSdkVersion 30

        versionCode 1

        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    }

    buildTypes {

        release {

            minifyEnabled false

            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

        }

    }

    compileOptions {

        sourceCompatibility JavaVersion.VERSION_1_8

        targetCompatibility JavaVersion.VERSION_1_8

    }

}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'

    implementation 'com.google.android.material:material:1.3.0'

    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

    testImplementation 'junit:junit:4.13.2'

    androidTestImplementation 'androidx.test.ext:junit:1.1.2'

    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    //add this dependency

    implementation 'com.karumi:dexter:4.2.0'

}

Alarm Manager Example in Android