如何服务于Android的使用IntentService重新启动 [英] How to restart Service using IntentService in android

查看:750
本文介绍了如何服务于Android的使用IntentService重新启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够阻止我ServiceA(它使用开始 AlarmManager )当 IntentService 通过发送运行从 IntentService 广播广播接收机。我想后,我的 IntentService 完成了他的工作再次启动相同ServiceA。

I am able to stop my ServiceA(it is started using AlarmManager) when an IntentService is running by sending a broadcast from IntentService to broadcast receiver. I want to Start the same ServiceA again after my IntentService finished his work.

前我有服务 SrvA ,IntentService IntSrvB 和广播接收器 MyBcr 。当我的 IntSrvB 运行,我能够停止 SrvA 。我的问题是如何重新启动 SrvA 时再我的 IntSrvB 完成他的工作。

Ex-I have Service SrvA,IntentService IntSrvB and BroadcastReceiver MyBcr.When my IntSrvB running i am able to stop SrvA.My problem is How to Restart SrvA again when my IntSrvB finish his work.

请注意 - SERVA 使用开始 AlarmManager

Note-ServA is started using AlarmManager.

推荐答案

编辑:根据您code,你可以通过你的变量 myIntent &安培; myIntent2 IntentService 类 - 然后你可以用它们来创建具有<$使用挂起的意图的复制品C $ C> AlarmManager 。

Based on your code, you can pass your variables myIntent & myIntent2 into your IntentService class - you can then use them to recreate exact replicas of the pending intents used with the AlarmManager.

请注意,它看起来像你设置为重复一次NOW,然后在之后的时间间隔各意向报警。然后,含蓄再次启动该服务,即启动服务两次NOW。这看起来像一个错误 - 看文档的<一个href=\"http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating%28int,%20long,%20long,%20android.app.PendingIntent%29\"相对=nofollow> AlarmManager.setRepeating()方法

Please note that it looks like you set an Alarm for each intent that repeats once "NOW" and then at intervals after that. You then implicitly start the service again i.e. you start the service twice "NOW". That looks like a mistake - look at the docs for the AlarmManager.setRepeating() method.

低于原来的答复...

Original answer below...

将这个到你的 IntentService

@Override
protected void onHandleIntent(Intent intent)
{
    try
    {
        // STOP SERVICE

        // DO YOUR WORK HERE
    }
    finally
    {
        // START SERVICE
    }

}

您已经拥有了code停止服务。您可以从广播接收器走启动服务code,并把它在finally块。

You already have the code to stop the service. You can take the "start service" code from your BroadcastReceiver and put it in the finally block.

我不建议重写的onDestroy()一般在Android上。

I would not recommend overriding onDestroy() in general on Android.

注意:我不认为这是设计您的应用程序的最佳方式,但我回答你的问题。就个人而言,我会在我的主服务的方法就是能够禁用和放大器;使其功能 - 然后调用该方法,而不是起点和放大器;停止服务。

NOTE: I don't think this is the best way to design your app, but I'm answering your question. Personally, I would have a method in my main Service that is able to disable & enable its functionality - and then call that method instead of starting & stopping the service.

这篇关于如何服务于Android的使用IntentService重新启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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