BroadcastReceiver + SMS_RECEIVED
You would also need to specify a uses-permission in your manifest file:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
The following tutorials should help:
React on incoming SMS
SMS messaging in Android
BroadcastReceiver SMS_Received not working on new devices
Okay the problem was resolved.
The issue did not reside with priorities, but with my phone being a Nexus 6P (a.k.a. API 23).
Providing permissions in the manifest.xml alone wasn't enough and I had to add code for runtime permission request. See Android documentation for runtime permissions
Add this code to your MainActiviy:
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.RECEIVE_SMS},
MY_PERMISSIONS_REQUEST_SMS_RECEIVE);
Define this at the top of MainActivity Class:
private int MY_PERMISSIONS_REQUEST_SMS_RECEIVE = 10;
And also add this override:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_PERMISSIONS_REQUEST_SMS_RECEIVE) {
// YES!!
Log.i("TAG", "MY_PERMISSIONS_REQUEST_SMS_RECEIVE --> YES");
}
}
Android : BroadcastReceiver SMS_RECEIVED
The action that you match in your BroadcastReceiver
should be android.provider.Telephony.SMS_RECEIVED
, not just the last part.
SMS received broadcastReceiver is not called when app is not running
Full working demo Runtime Permissions
Change the permission name which you want to get at runtime.
private String permission = Manifest.permission.RECEIVE_SMS;
Those permssions which you want to get at runtime should also be declared in
manifest too.
public class MainActivity extends AppCompatActivity {
private Context context;
private Button button;
private static final int REQUEST_RUNTIME_PERMISSION = 123;
private String permission = Manifest.permission.RECEIVE_SMS;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (CheckPermission(MainActivity.this, permission)) {
// you have permission go ahead
YouCanReadContactNow();
} else {
// you do not have permission go request runtime permissions
RequestPermission(MainActivity.this, permission, REQUEST_RUNTIME_PERMISSION);
}
}
});
}
private void YouCanReadContactNow() {
}
@Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {
switch (permsRequestCode) {
case REQUEST_RUNTIME_PERMISSION: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// you have permission go ahead
YouCanReadContactNow();
} else {
// you do not have permission show toast.
}
return;
}
}
}
public void RequestPermission(Activity thisActivity, String Permission, int Code) {
if (ContextCompat.checkSelfPermission(thisActivity,
Permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Permission)) {
} else {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Permission},
Code);
}
}
}
public boolean CheckPermission(Context context, String Permission) {
if (ContextCompat.checkSelfPermission(context,
Permission) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
}
layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/base"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="horizontal">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:text="Request contact permissions"
android:textSize="20dp" />
</RelativeLayout>
SMS Broadcast Receiver not called on Android 6
Are you using a default messaging app with "Disable other apps" flag on?
Please see this:
"android.provider.Telephony.SMS_RECEIVED" not working on my device (HTC Wildfire) - how to debug?
Edit:
Since you are using Android 6, you should use the new permissions model. Check this out:
http://developer.android.com/training/permissions/requesting.html
Related Topics
How to Use Setarguments() and Getarguments() Methods in Fragments
Sliding Drawer Appear in All Activities
Delete Alarm from Alarmmanager Using Cancel() - Android
Android Setonclicklistener Method - How Does It Work
Resizing Layouts Programmatically (As Animation)
Linkedlist Put into Intent Extra Gets Recast to Arraylist When Retrieving in Next Activity
Which Correct Flag of Autoconnect in Connectgatt of Ble
How to Solve Execution Failed for Task ':App:Compileflutterbuilddebug'
Android: How to Make the Keypad Always Visible
How to Make Notification Intent Resume Rather Than Making a New Intent
Android: Programmatically Detect If Device Has Hardware Menu Button
Android: Implementing Progressbar and "Loading..." for Endless List Like Android Market
Creating Custom Lockscreen in Android
How to Crop an Image in Android
Ndk Resolution Outcome: Project Settings: Gradle Model Version=5.4.1, Ndk Version Is Unknown Error
Drawerlayout Double Drawer (Left and Right Drawers Simultaneously)