My Broadcast receiver is not working on oreo but its working below oreo it's working fine, I searched a lot regarding this but could not find the suitable solution. Does anyone face the same problem, here is my code regarding my service in which broadcast has been implemented. Kindly suggests me that how I can make in working in oreo.
Here is the class
public int onStartCommand(Intent intent, int flags, int startId) {
mContext = this;
mAppPreferences = new AppPreferences(mContext);
if (intent.getExtras() != null) {
data = (String) intent.getExtras().get("showPopUp");
phoneNumber= (String) intent.getExtras().get("dialNumber");
}
final IntentFilter intentFilter = new IntentFilter();
if (data.equalsIgnoreCase("true")) {
showPopup(getApplicationContext());
Utils.ApiHit(phoneNumber,getApplicationContext());
}
intentFilter.setPriority(2147483647);
intentFilter.addAction("android.intent.action.PHONE_STATE");
callExplicitReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
} else {
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
phoneNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
int state = 0;
if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
state = TelephonyManager.CALL_STATE_IDLE;
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
state = TelephonyManager.CALL_STATE_OFFHOOK;
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
state = TelephonyManager.CALL_STATE_RINGING;
}
onCallStateChanged(context, state, phoneNumber);
}
}
}
};
mContext.registerReceiver(callExplicitReceiver, intentFilter);
return START_NOT_STICKY;
}
public void onIncomingCallReceived(Context ctx, String number, Date start) {
}
public void onIncomingCallAnswered(Context ctx, String number, Date start) {
if (popupView.getVisibility() == View.GONE) {
popupView.setVisibility(View.VISIBLE);
}
}
public void onIncomingCallEnded(Context ctx, String number, Date start, Date end) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
DeleteCallLogByNumber(number);
}
}, 2000);
if (popupView.getVisibility() == View.VISIBLE) {
popupView.setVisibility(View.GONE);
}
}
public void onOutgoingCallStarted(Context ctx, String number, Date start) {
// mAppPreferences.setPrefrenceString("busy", "yes");
// if (data.equalsIgnoreCase("true")) {
mediaPlayer = MediaPlayer.create(ctx, R.raw.speech_audio);
// } else {
// mediaPlayer = MediaPlayer.create(ctx, R.raw.speech_audio);
// }
mediaPlayer.start();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
}, 12000);
if (popupView.getVisibility() == View.GONE) {
popupView.setVisibility(View.VISIBLE);
}
}
public void onOutgoingCallEnded(Context ctx, String number, Date start, Date end) {
mAppPreferences.setPrefrenceString("busy", "no");
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
DeleteCallLogByNumber(phoneNumber);
}
}, 2000);
if (popupView.getVisibility() == View.VISIBLE) {
popupView.setVisibility(View.GONE);
}
}
public void onMissedCall(Context ctx, String number, Date start) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
DeleteCallLogByNumber(phoneNumber);
}
}, 2000);
if (popupView.getVisibility() == View.VISIBLE) {
popupView.setVisibility(View.GONE);
}
}
public void onCallStateChanged(Context context, int state, String number) {
if (lastState == state) {
return;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
callStartTime = new Date();
savedNumber = number;
onIncomingCallReceived(context, number, callStartTime);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if (lastState != TelephonyManager.CALL_STATE_RINGING) {
isIncoming = false;
callStartTime = new Date();
onOutgoingCallStarted(context, savedNumber, callStartTime);
} else {
isIncoming = true;
callStartTime = new Date();
onIncomingCallAnswered(context, savedNumber, callStartTime);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (popupView.getVisibility() == View.VISIBLE) {
popupView.setVisibility(View.GONE);
}
if (lastState == TelephonyManager.CALL_STATE_RINGING) {
onMissedCall(context, savedNumber, callStartTime);
} else if (isIncoming) {
onIncomingCallEnded(context, savedNumber, callStartTime, new Date());
} else {
onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());
}
break;
}
lastState = state;
}
@Override
public void onDestroy() {
mContext.unregisterReceiver(callExplicitReceiver);
}
Noting is in coming inside receiever,Can anyone help me out in this?
New Additions as per discussion
Manifest data :-
Permission used :-
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Reciver:-
<receiver android:name="com.example.dialer.AppUtils.StartUpBootReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
My BroadCast Reciever Class :-
public class StartUpBootReceiver extends BroadcastReceiver {
private Context mContext;
@Override
public void onReceive(Context context, Intent intent) {
mContext= context;
String action = "START";
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
context.startForegroundService(new Intent(context, PhoneStateService.class));
}
else
{
context.startService(new Intent(context, PhoneStateService.class));
}
}
}
private boolean isServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
Rest the same service will get the call, but the problem is i still does not get call in receiver.And m primary point is that service should only get called once user tap on button , not automatically as i have to pass some values in the service.
Thanks
Open the Phone app and tap Keypad, then type *#0*#. A diagnostic screen pops up with buttons for a variety of tests. Tap Red, Green, or Blue to test those pixel colors. Tap Receiver to check the audio, Vibration to try the vibrating feature, or Sensor to test the accelerometer and other sensors.
When you Register for any Broadcast receiver by Default onReceive() method will be called on the Main Thread( UI Thread). But If we want to do any heavy operations or if your broadcast is calling multiple times, then so much load will be accumulated on Main Thread which is not recommended.
Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.
The problem comes with the service you're trying to run, services or persistent background services are not permitted to run for long for apps targeting Oreo and above.
Check this guide and this as well for migrating your app to support Oreo.
Broadcast Limitations
If an app registers to receive broadcasts, the app's receiver consumes resources every time the broadcast is sent. This can cause problems if too many apps register to receive broadcasts based on system events; a system event that triggers a broadcast can cause all of those apps to consume resources in rapid succession, impairing the user experience. To mitigate this problem, Android 7.0 (API level 25) placed limitations on broadcasts, as described in Background Optimization. Android 8.0 (API level 26) makes these limitations more stringent.
Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest. An implicit broadcast is a broadcast that does not target that app specifically. For example, ACTION_PACKAGE_REPLACED is an implicit broadcast, since it is sent to all registered listeners, letting them know that some package on the device was replaced. However, ACTION_MY_PACKAGE_REPLACED is not an implicit broadcast, since it is sent only to the app whose package was replaced, no matter how many other apps have registered listeners for that broadcast.
Apps can continue to register for explicit broadcasts in their manifests.
Apps can use Context.registerReceiver() at runtime to register a receiver for any broadcast, whether implicit or explicit.
Broadcasts that require a signature permission are exempted from this restriction, since these broadcasts are only sent to apps that are signed with the same certificate, not to all the apps on the device.
Check Official Documentation here
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With