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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…