再次运行服务,但是一旦其当前执行完成 [英] Run service again but Once its current execution is finished

查看:89
本文介绍了再次运行服务,但是一旦其当前执行完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将尝试使其简短而简单.我有一个foreground service Abroadcast receiver.有时它两次调用Service B.

I will try to keep it short and simple. I have a foreground service A with broadcast receiver. Some times it calls Service B twice.

我希望service B首先完成它的执行,并且如果另一个请求再次运行它,它应该一直等待直到更早的执行完成.这就是为什么我将服务B创建为IntentService的原因.但是由于呼叫日志仍未更新,因此我正在使服务B休眠2秒钟.

I want service B to finish its execution first and if another request comes to run it again, it should remain awaiting until earlier execution finish. Thats why i have created Service B as IntentService. But as call log is still not updated, i am sleeping service B for 2 seconds.

但是当我在IntentService中实现runnable ...时,它保留了其原始行为,如果另一个请求到来,则两个请求同时在运行Service B.

But when i implemented runnable in IntentService..., It left its original behaviour and if another request comes, both requests are running Service B concurrently.

我的服务广播接收器:

Log.d("RECEIVER X: ", "CALL ENDS HERE...");
Intent SendSMS = new Intent(context, SendSMS.class);
startService(SendSMS);


我的IntentService:


My IntentService :

    public class SendSMS extends IntentService
    {
    public Boolean IsRunning = false;

    public SendSMS()
    {
    super("SendSMS");
    Log.d("SendSMS : ", "\nOnCreate...");
    }

    @Override
    protected void onHandleIntent(Intent intent)
    {

        new Thread(new Runnable()
        {
            public void run()
            {

                if(IsRunning == false)
                {
                    IsRunning = true;
                    Thread.sleep(2000);
                    // perform operations here...
                }
            }
        }).start();
    }
    }


我的操作应该执行两次,但是首先要完成2秒钟的睡眠.任何帮助将不胜感激.谢谢进阶


My operations should be performed twice , But after first is finished with sleep of 2 seconds. Any help will be appreciated. Thanking you in advanced

推荐答案

我的应用程序的目标是api 17的api级别26.但是与Oreo一样,它也不允许实现静态接收器. (像BOOT_COMPLETED这样的意图动作仍然很少被接受.但是我想实现PHONE_STATE.根据文档,我已经在前台服务中实现了它.如下所示:

My app targets api level 26 from api 17. But as in and above Oreo it is not allowed to implement static receivers. ( There are few intent actions like BOOT_COMPLETED are still accepted. But I wanted to implement PHONE_STATE. As per documentation i have implemented it in a foreground service. as below :

前景服务中的运行时接收器:

Runtime Receiver in Foreground Service :

private BroadcastReceiver mCallBroadcastReceiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();

        if (action.equalsIgnoreCase("android.intent.action.PHONE_STATE"))
        {
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING))
            {
                Log.d("RECEIVER X: ", "INCOMING CALL...");
            }
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE))
            {
                Log.d("RECEIVER X: ", "CALL ENDS HERE...");
                Intent Dispatcher = new Intent(context, CatchNumbers.class);
                startService(Dispatcher);
            }
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
            {
                Log.d("RECEIVER X: ", "ACTIVE CALL GOING ON...");
            }
        }
    }
};


在这里您可以看到,我正在呼叫我的CatchNumbers.是的,我一次调用,但仅在ANDROID 5.0和5.1 Google BUG中调用,它触发了两次[c8]. Hence was the above question Posted here如何禁止两个并发线程运行同一服务.. !!


here you can see, i am calling my CatchNumbers. YES I AM CALLING ONCE, BUT AS ONLY ON IN ANDROID 5.0 AND 5.1 GOOGLE BUG, IT TRIGGERS ALL_STATES TWICE TWICE. Hence was the above question Posted here that how to disallow two concurrent threads running the same service..!!

CatchNumbers内:

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    new Thread(new Runnable()
    {
        public void run()
        {
            try
            {
                Thread.sleep(2000);
                Log.d("CatchNumbers : ", "\nDispatched Call ...");

                Intent SendSMS = new Intent(CatchNumbers.this, SendSMS.class);
                startService(SendSMS);
            }
            catch (InterruptedException e)
            {
                Log.d("CatchNumbers : ", "Thread : InterruptedException Error in service...\n");
                Log.e("CatchNumbers : ", "Exception is : ", e);
            }
        }
    }).start();

    return super.onStartCommand(intent, flags, startId);
}

在并发调用相同的SendSMS服务之前,这里在2秒的可运行睡眠中,然后仅将其调度为运行IntentService工作线程...

Before heading concurrent calls to same SendSMS service, here within runnable sleep of 2 seconds and then only dispatch it to run IntentService worker thread...

现在,由于Intent Service不允许运行多个实例...其他请求仍在等待,直到第一个SendSMS执行完成.现在,尊敬的XY LOVER ..,由于android 5.0和5.1触发了两次相同的事件,因此调用任何服务都将是有问题的..,这导致数据丢失...因此,决定改为从CALL_LOG获取它.一旦通话状态变为空闲.同样,并发调用该意图服务的相同问题也因此得以解决.我也尝试在Intent服务中实现可运行的内容,但是由于这个原因,Intent服务失去了保持下一个请求等待当前执行结束的属性.并且两个请求同时运行IntentService导致数据丢失

Now as Intent Service does not allow to run multiple instances... Other request remains awaiting until first SendSMS execution gets completes. Now, Dear XY LOVER.., As android 5.0 and 5.1 triggers twice the same event it would have been problematic to call any service.., which was causing data loss... So decided to get it from CALL_LOG instead. Once call state goes IDLE. Again same problem of concurrent calls to that intent service so solved it like this. I also tried implementing runnable in Intent service but because of it Intent service lost its property of keeping next request in waiting until current execution ends. And Both of requests were running IntentService Concurrently causing data loss


现在我的Intent服务没有可运行的方法.


Now My Intent Service is not having Runnable method.

这篇关于再次运行服务,但是一旦其当前执行完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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