i recently updated my app to support android 6 marshmallow. i followed the instruction on https://developer.android.com/training/permissions/requesting.html
and add
Android 6.0 / SDK 23 introduces a new way of requesting permissions.
You need to request the SMS permission, see the link below for how to handle permissions:
https://developer.android.com/training/permissions/index.html
You need a permission for api level 23+, google reworked the permission system so the app user can grant and revoke permissions after installing your app
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
if(Build.VERSION.SDK_INT < 23){
//your code here
}else {
requestContactPermission();
}
private void requestContactPermission() {
int hasContactPermission =ActivityCompat.checkSelfPermission(context,Manifest.permission.RECEIVE_SMS);
if(hasContactPermission != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(Context, new String[] {Manifest.permission.RECEIVE_SMS}, PERMISSION_REQUEST_CODE);
}else {
//Toast.makeText(AddContactsActivity.this, "Contact Permission is already granted", Toast.LENGTH_LONG).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
// Check if the only required permission has been granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i("Permission", "Contact permission has now been granted. Showing result.");
Toast.makeText(this,"Contact Permission is Granted",Toast.LENGTH_SHORT).show();
} else {
Log.i("Permission", "Contact permission was NOT granted.");
}
break;
}
}
You need to add the permission into manifest xml:
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
AND, you need to ask for the permission at runtime. Until android 6, the permissions were granted automatically on installation. In android 6 and above, you can install application and not grant the permission. You can use this function in your activity class:
private void requestSmsPermission() {
String permission = Manifest.permission.RECEIVE_SMS;
int grant = ContextCompat.checkSelfPermission(this, permission);
if ( grant != PackageManager.PERMISSION_GRANTED) {
String[] permission_list = new String[1];
permission_list[0] = permission;
ActivityCompat.requestPermissions(this, permission_list, 1);
}
}
this - your activity.
You must grant SMS permission to your app after installation. Just go to Settings > Apps > Your_app > Permissions and then grant the required permission.
The Android 6 runtime permission android.provider.Telephony.SMS_RECEIVED
gives you permission to receive that message when it is sent by the system SMS provider.
You however are trying to broadcast that message yourself. I'm not sure that is permitted, and as you have found is not controlled by the same permission. (In fact, I assume that it has been locked down on Marshmallow so that only the system is able to notify apps of received SMS messages).