1

I have an old application in which the service was invoked using the AlarmManager's setExact every minute, but in the latest Android O, it is not allowing me to run the service in the same way. I couldn't use foreground service because I don't want to show any notification when my service is running.

I want help on tweaking my application architecture in a way so that my code should run in the Android O with minimum changes.

When I run my application in the Android, it is crash:(.Please find the stack trace of the crash below.

java.lang.RuntimeException: Unable to create application com.***.***.***ControllerApplication: java.lang.IllegalStateException: Not allowed to start service Intent

Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.***.***.qa/com.***.***.services.AlarmService

Edit:

The task in my application is time critical therefore I used setExact in AlarmManager but none of the currently supported APIs is scheduling jobs at an exact time. With that said, If I use JobScheduler/ WorkManager then my application will malfunction.

Naresh Sharma
  • 4,323
  • 7
  • 48
  • 68
Syed Taruf Naqvi
  • 507
  • 3
  • 18

1 Answers1

-1

You can use the Alarm manager which will be called after every 10 mins

        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Random random = new Random();
        int m = random.nextInt(9999 - 1000) + 1000;

        Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
        notificationIntent.setClass(this,AlarmReceiver_.class);
        notificationIntent.addCategory("android.intent.category.DEFAULT");

        PendingIntent broadcast = PendingIntent.getBroadcast(YourClass.this, r5, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + 300000L,
                600000L, broadcast);

ManiFest receiver Code where you will get a receiver response

<receiver android:name="com.yourpackage.AlarmReceiver_"

        >
        <intent-filter>
            <action android:name="android.media.action.DISPLAY_NOTIFICATION" />
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <action android:name="android.intent.action.REBOOT" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />

        </intent-filter>
    </receiver>

You will have to create Receiver where you will receive data as an above-specified name of AlarmReceiver_.class

ॐ Rakesh Kumar
  • 1,318
  • 1
  • 14
  • 24