When I restart the phone I set the alarm but it does not run

Viewed 16

I have an app developed in java whose function is to capture the location when the app is active, background, destroyed and when the phone restarts

I already managed to capture the location when the app is active, background and destroyed but I am trying to do it when the phone reboots and create the task to capture but it says that the task was configured well but it does not execute it every 1 minute

My Manifest

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

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.WAKE_LOCK"/>

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Rutas"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyForegroundServices"></service>
        <receiver android:name=".MyBroadcastReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
        </receiver>
    </application>

</manifest>

My Receiver

public class MyBroadcastReceiver extends BroadcastReceiver {
    GPSTracker gps;
    Context alarmContext;

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.e("onReceive", " Init Receive" );
        alarmContext = context;

        gps = new GPSTracker(context);
        double latitude = gps.getLatitude();
        double longitude = gps.getLongitude();
        Log.e("Location", "Lat: " + latitude + "\n Long: " + longitude);

        if(intent.getAction() != null){

            if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
                Log.e("Boot Completed", "Boot Completed");

                setAlarmOnBoot();

                Intent serviceIntent = new Intent(context, MyForegroundServices.class);
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
                    context.startForegroundService(serviceIntent);
                }else{
                    context.startService(serviceIntent);
                }
            }
        }

    }

    public void setAlarmOnBoot(){
        Log.e("Alarm Boot", "Initialize task");
        try{
            AlarmManager alarm = (AlarmManager) alarmContext.getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(alarmContext, MyBroadcastReceiver.class);
            PendingIntent pIntent = PendingIntent.getBroadcast(alarmContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 1000*60*1, pIntent);

            Log.e("Success", "Alarm has been configured successfully");

        } catch (Exception ex){
            Log.e("Failed Try Receiver", ex.getMessage());
        }
    }
}

I am testing the app on a samsung A13 with android 12

0 Answers
Related