pierwsze pozwolenie RECEIVE_SMS muszą być zadeklarowane w AndroidManifest.xml.
...
<uses-permission android:name="android.permission.RECEIVE_SMS" />
...
<receiver
android:name=".receiver.IncomingSmsReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Następnie z poziomu interfejsu API 23 musimy zażądać uprawnienia RECEIVE_SMS w czasie wykonywania. Ważne jest, aby to zauważyć. https://developer.android.com/training/permissions/requesting.html
public class MainActivity extends AppCompatActivity {
private static final int PERMISSIONS_REQUEST_RECEIVE_SMS = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Request the permission immediately here for the first time run
requestPermissions(Manifest.permission.RECEIVE_SMS, PERMISSIONS_REQUEST_RECEIVE_SMS);
}
private void requestPermissions(String permission, int requestCode) {
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(this, permission)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
Toast.makeText(this, "Granting permission is necessary!", Toast.LENGTH_LONG).show();
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{permission},
requestCode);
// requestCode is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSIONS_REQUEST_RECEIVE_SMS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
NotificationUtil.getInstance().show(this, NotificationUtil.CONTENT_TYPE.INFO,
getResources().getString(R.string.app_name),
"Permission granted!");
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
NotificationUtil.getInstance().show(this, NotificationUtil.CONTENT_TYPE.ERROR,
getResources().getString(R.string.app_name),
"Permission denied! App will not function correctly");
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
}
Nadzieja to pomóc.
Spróbuj zawierania przed tagiem –
@ Vaibhav .. próbowałem tego ... Umieściłem go w różnych miejscach: -/ –
spróbuj wprowadzić następujące zmiany w odbiorniku, a jeśli czytasz SMS, możesz dodać także < intent-filter android: priority = "1000"> intent-filter> –