Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.


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.


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


My Service Broadcast receiver :

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

My IntentService :

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

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

    @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();
    }
    }

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
187 views
Welcome To Ask or Share your Answers For Others

1 Answer

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...");
            }
        }
    }
};

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..!!


Within 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 : ", "
Dispatched Call ...");

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

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

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...


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


Now My Intent Service is not having Runnable method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...