Android的 - 立即启动报警服务?Android

2023-09-05 05:11:17 作者:眼泪都在笑

我已经创建了一个在启动接收器反复调用清醒的意图服务每5分钟,但无法弄清楚如何立即启动服务安装应用程序时..?我不想依靠重启他们的设备的用户就开始运行之前!

I have created an On Boot Receiver to repeatedly call a wakeful intent service every 5 minutes but cannot figure out how to start the service immediately when the app is installed..? I do not want to rely on the user rebooting their device before it starts to run!

下面是我的code到目前为止:

Here is my code so far :

public class OnBootReceiver extends BroadcastReceiver {
private static final int PERIOD = 300000; // check every 5 minutes

@Override
public void onReceive(Context context, Intent intent) {
    AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, OnAlarmReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);

    mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60000, PERIOD, pi);
}}

任何人都可以帮我请? :)

Can anyone help me out pls? :)

推荐答案

如果你想设置一个alarmmanager安装应用程序时要启动的服务,那么它是不可能的。这是一个操作系统的限制,安全性,如果你会的。但是,如果你想开始在当下的应用程序启动该服务,只需调用它,它将保持乳宁。

If you want to set an alarmmanager to start your service when the app is installed, then it's not possible. It's a OS limitation, security if you will. But if you want to start the service in the moment the app starts, just call it, it will keep runing.