Broadcast Receivers not working in Android 6.0 Marshmallow

I just updated my Nexus 5 to android 6, until now my app was working fine, but now the broadcast receivers are not working. Has something changed in the new version?
Here is the code I tried that was working on previous versions but not in marshmallow –

Android Manifest

  • Cordova App - build tool error - Gradle
  • difficulty in importing the facebook-android-sdk into eclipse
  • How to pass ArrayList<CustomeObject> from one activity to another?
  • Style android spinner
  • Can't detect Android in web server
  • how to clear surface holder when media player is finished?
  •     <intent-filter >
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <uses-permission android:name="android.permission.READ_SMS" ></uses-permission>
    

    Broadcast Receiver

    public String TAG ="someClass";
    private static String ACTION_SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equalsIgnoreCase(ACTION_SMS_RECEIVED)) {
            Log.d(TAG, "Received...");
        }
    }
    

    Service

    Broadcast_receiver broadcast_receiver = new Broadcast_receiver();
    IntentFilter filter1 = new IntentFilter();
    filter1.addAction("android.provider.Telephony.SMS_RECEIVED");
    registerReceiver(broadcast_receiver, filter1);
    

    Similarly the broadcast receiver for PHONE_STATE is also not working.

  • “The item you were attempting to purchase could not be found” Android in-app billing
  • Show ImageView programmatically
  • Monkey made my app meow
  • Force RTL layout direction not working for app
  • Android: Access child views from a ListView
  • Can I set an attached device as default launch target?
  • 3 Solutions collect form web for “Broadcast Receivers not working in Android 6.0 Marshmallow”

    Your app’s target API level is 23, that is android M (6.0). In android M there are huge changes related to user-permissions.
    Here is nice article explaining the changes.

    As stated in Android – Requesting Permissions

    Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app… The user can revoke the permissions at any time…

    It’s also stated that:

    System permissions are divided into two categories, normal and dangerous:

    1. Normal permissions do not directly risk the user’s privacy. If your app lists a normal permission in its manifest, the system grants the permission automatically

    2. Dangerous permissions can give the app access to the user’s
      confidential data. If you list
      a dangerous permission, the user has to explicitly give approval to
      your app

    Here are full lists of Dangerous Permissions and Normal Permissions

    All that basically means that you need to manually request for any dangerous permission, when it’s actually needed.

    Since it potentially might be needed multiple times in your code, you can create a reusable method that checks whether specific permission is granted already and if it’s not – to request it from user.

    Here an example:

    public class PermissionManager {
        //A method that can be called from any Activity, to check for specific permission
        public static void check(Activity activity, String permission, int requestCode){
            //If requested permission isn't Granted yet 
            if (ActivityCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
                //Request permission from user
                ActivityCompat.requestPermissions(activity,new String[]{permission},requestCode);
            }
        }
    }
    

    Usage example:

    //Inside your activity:
    //1. Define static constant for each permission request
    public static final int REQUEST_CODE_FOR_SMS=1;
    //2. When needed (for example inside .onStart event) use method PermissionManager.check for requested permission 
    @Override
    protected void onStart() {
        super.onStart();
        PermissionManager.check(this, Manifest.permission.RECEIVE_SMS, REQUEST_CODE_FOR_SMS);
    }
    //3. Handle User's response for your permission request
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if(requestCode==REQUEST_CODE_FOR_SMS){//response for SMS permission request
            if(grantResults[0]==PackageManager.PERMISSION_GRANTED){
                //What to do if User allowed SMS permission
            }else{
                //What to do if user disallowed requested SMS permission
            }
        }
    }
    

    Note:

    1. If you need to use PermissionManager.check inside Fragment instance, use: getActivity() as its first parameter.

    2. You can use checkSelfPermission inside Service instance, to check if some permission is granted already, but not requestPermissions to request it. Because checkSelfPermission can be used for any Context, but requestPermissions only for Activity

    Marshmallow is blocking the dangerous permissions.

    This doesn’t apply to the scenario listed, but it might help someone else. I kept coming to this SO for why some of our Broadcast Receiver’s weren’t working. We have a custom permission setup and had the android:protectionLevel="dangerous". Changed it to android:protectionLevel= "signature"and everything started working.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.