Android 如果是';它没有使用AlarmManager运行

Android 如果是';它没有使用AlarmManager运行,android,service,alarmmanager,Android,Service,Alarmmanager,我有一个由AlarmManager启动的服务,就像这样 Intent in = new Intent(SecondActivity.this,BotService.class); PendingIntent pi = PendingIntent.getService(SecondActivity.this, 9768, in,PendingIntent.FLAG_UPDATE_CURRENT); AlarmMana

我有一个由AlarmManager启动的服务,就像这样

Intent in = new Intent(SecondActivity.this,BotService.class);
                    PendingIntent pi = PendingIntent.getService(SecondActivity.this, 9768, in,PendingIntent.FLAG_UPDATE_CURRENT);

                    AlarmManager alarms = (AlarmManager) SecondActivity.this.getSystemService(Context.ALARM_SERVICE);
                    alarms.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),pi);
当服务启动时,它会再次设置AlarmManager,我这样做是因为我需要随机间隔的定期服务

这是通过以下方式实现的:

Intent in = new Intent(BotService.this, BotService.class);
            PendingIntent pi = PendingIntent.getService(BotService.this, 9768,
                    in, PendingIntent.FLAG_UPDATE_CURRENT);

            AlarmManager alarms = (AlarmManager) BotService.this.getSystemService(Context.ALARM_SERVICE);
            alarms.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+ (attack_interval * MINS) + attack_interval_min, pi);
我的服务可能会持续一段随机时间,如果服务未运行,如何通过AlarmManager启动服务


如果它正在运行,我需要再次设置AlarmManager,因为以其他方式,我的服务不会再次启动。谢谢你的回答。如果有任何不清楚的地方,请询问

如果您查看文档。当服务启动时,它有一个startID,这可用于确定在给定时间运行的服务实例的数量

参考:

public int onStartCommand (Intent intent, int flags, int startId)

Added in API level 5
Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request. Do not call this method directly.

For backwards compatibility, the default implementation calls onStart(Intent, int) and returns either START_STICKY or START_STICKY_COMPATIBILITY.

If you need your application to run on platform versions prior to API level 5, you can use the following model to handle the older onStart(Intent, int) callback in that case. The handleCommand method is implemented by you as appropriate:

如果你看一下文档。当服务启动时,它有一个startID,这可用于确定在给定时间运行的服务实例的数量

参考:

public int onStartCommand (Intent intent, int flags, int startId)

Added in API level 5
Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request. Do not call this method directly.

For backwards compatibility, the default implementation calls onStart(Intent, int) and returns either START_STICKY or START_STICKY_COMPATIBILITY.

If you need your application to run on platform versions prior to API level 5, you can use the following model to handle the older onStart(Intent, int) callback in that case. The handleCommand method is implemented by you as appropriate:

您可能希望实现一个新的;如果该方法适合您的需要,那么您将摆脱所有生命周期的麻烦

另外,为什么不通过或设置repeating设置重复报警?当然,只有当您确信您的IntentService的运行时间永远不会超过报警间隔,或者如果您有另一种合理的机制在IntentService仍在运行时忽略报警,您才会这样做


更新:如果您还询问如何开始您的服务。。。那要看情况了。许多人认为,
BOOT\u COMPLETED
广播是解决一切问题的解决方案。但是,它会阻止用户将应用程序移动到SD卡。如果只有在应用程序运行后,您才能使用重复运行的服务,则应用程序可以启动此操作。另外,如果您查看系统发送的哪些其他事件可能与您的服务相关,例如internet连接的更改,则仍然可以避免
BOOT_COMPLETED
陷阱。如果您决定执行
引导\u COMPLETED
,您可以找到一个示例。

您可能希望实现一个;如果该方法适合您的需要,那么您将摆脱所有生命周期的麻烦

另外,为什么不通过或设置repeating设置重复报警?当然,只有当您确信您的IntentService的运行时间永远不会超过报警间隔,或者如果您有另一种合理的机制在IntentService仍在运行时忽略报警,您才会这样做


更新:如果您还询问如何开始您的服务。。。那要看情况了。许多人认为,
BOOT\u COMPLETED
广播是解决一切问题的解决方案。但是,它会阻止用户将应用程序移动到SD卡。如果只有在应用程序运行后,您才能使用重复运行的服务,则应用程序可以启动此操作。另外,如果您查看系统发送的哪些其他事件可能与您的服务相关,例如internet连接的更改,则仍然可以避免
BOOT_COMPLETED
陷阱。如果您决定
BOOT\u COMPLETED
,您可以找到一个示例。

您可以注册您的应用程序到系统的启动事件,这样无论何时系统启动,您都有机会发出警报。您可以注册您的应用程序到系统的启动事件,所以无论何时系统启动,您有机会发出警报。所以我尝试使用onStartCommand执行一些操作,我想到了当服务启动时,如果已经运行OnCreate的服务没有执行。所以我在onStart命令中检查在OnCreate中设置的布尔值。这样我就可以确定服务是否正在运行。谢谢。很高兴听到它对@user1356158有帮助,所以我试着用onStartCommand做了一些事情,我想到了如果已经运行OnCreate的服务在服务启动时没有执行的话。所以我在onStart命令中检查在OnCreate中设置的布尔值。这样我就可以确定服务是否正在运行。谢谢。很高兴听到@user1356158有帮助