Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am not able to receive “android.provider.Telephony.SMS_RECEIVED” this broadcast in Android Oreo

This is my Menifest file

<receiver
android:name="com.agribazaar.android.receivers.OTPReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

This is my Broadcast Receiver class

public class OTPReceiver extends BroadcastReceiver {
   @Override
public void onReceive(Context context, Intent intent) {        
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){

}
}
like image 400
Danish Farooq Avatar asked Dec 06 '17 12:12

Danish Farooq


1 Answers

This fixed the issue for me - I wasn't explicitly requesting permission at runtime for android.Manifest.permission.RECEIVE_SMS. In earlier versions of android it was working fine but in android O devices i got the issue.

int SMS_PERMISSION_REQ_CODE_SUBMIT = 101;
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.RECEIVE_SMS)
                            != PackageManager.PERMISSION_GRANTED){

        ActivityCompat.requestPermissions(SmsActivity.this, new String[]{Manifest.permission.RECEIVE_SMS},
                                SMS_PERMISSION_REQ_CODE_SUBMIT);
}
like image 183
Deepak Joshi Avatar answered Oct 20 '22 05:10

Deepak Joshi