Android的 - 立即启动报警服务? [英] Android - start alarm service immediately?

查看:124
本文介绍了Android的 - 立即启动报警服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个在启动接收器反复调用清醒的意图服务每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.

这篇关于Android的 - 立即启动报警服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆