Run Service Again But Once Its Current Execution Is Finished
Solution 1:
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
publicvoidonReceive(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
:
@OverridepublicintonStartCommand(Intent intent, int flags, int startId)
{
newThread(newRunnable()
{
publicvoidrun()
{
try
{
Thread.sleep(2000);
Log.d("CatchNumbers : ", "\nDispatched Call ...");
IntentSendSMS=newIntent(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();
returnsuper.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.
Post a Comment for "Run Service Again But Once Its Current Execution Is Finished"