简体   繁体   English

如何每分钟在Android O中运行后台服务?

[英]How to run the background service in Android O, every minute?

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. 我有一个旧的应用程序,其中每分钟使用AlarmManagersetExact调用该服务,但是在最新的Android O中 ,它不允许我以相同的方式运行该服务。 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. 我需要有关调整应用程序架构的帮助,以便我的代码可以在Android O中以最少的更改运行。

When I run my application in the Android, it is crash:(.Please find the stack trace of the crash below. 当我在Android中运行应用程序时,它是崩溃:(.。请在下面找到崩溃的堆栈跟踪。

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. 我的应用程序中的任务是时间紧迫的,因此我在AlarmManager使用了setExact ,但是当前所支持的API均未在准确的时间安排作业。 With that said, If I use JobScheduler/ WorkManager then my application will malfunction. 话虽如此, 如果我使用JobScheduler / WorkManager,则我的应用程序将出现故障。

You can use the Alarm manager which will be called after every 10 mins 您可以使用警报管理器,每10分钟会调用一次

        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 ManiFest接收器代码,您将在其中获得接收器响应

<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 您将必须创建Receiver,以上面指定的AlarmReceiver_.class名称接收数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM