My BroadcastReceiver does not work in my Activity

4

I want to receive an alert when I get a sms and I intend to create it in the following way:

public class MainActivity extends Activity {


    BroadcastReceiver receiver = null ;
    IntentFilter intentFilter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


       try {
            intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");

           Log.i("DESARROLLO","DESARROLLO111 ");
           receiver = new BroadcastReceiver() {
               @Override
               public void onReceive(Context context, Intent intent) {
                   Log.i("DESARROLLO","PROBANDO ONRECEIVE ");
                   processReceive(context, intent);
               }
           };
           Log.i("DESARROLLO","DESARROLLO1112222 ");
           registerReceiver(receiver,intentFilter);
       }catch (Exception e){
           Log.e("TAGERROR","ERROR CON CODIGO: "+ e);
       }

    }

     public void onResume(){
         Log.i("DESARROLLO","onResume ");
         super.onResume();
         registerReceiver (receiver,intentFilter);
     }
     public void onPause(){
         Log.i("DESARROLLO","onPause ");
         super.onPause();
         unregisterReceiver(receiver);
     }

    public void onDestroy() {
        super.onDestroy();
        Log.i("DESARROLLO","onDestroy ");
        unregisterReceiver(receiver);
    }

    public void processReceive(Context context, Intent intent) {
        Log.i("DESARROLLO","processReceive ");
        Toast.makeText(context, "PROBANDO ", Toast.LENGTH_LONG).show();

    }
}

and in the Manifiest

<uses-permission android:name="android.permission.RECEIVE_SMS"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity" >
           <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

    
asked by Lui5 07.11.2016 в 16:55
source

1 answer

1

You have your implementation correct but you must remember that for Android 6.0 or later, you need to obtain SMS permissions , for this you can use the following method:

private void checkSMSStatePermission() {
    int permissionCheck = ContextCompat.checkSelfPermission(
            this, Manifest.permission.RECEIVE_SMS);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        Log.i("Mensaje", "No se tiene permiso para enviar/recibir SMS.");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECEIVE_SMS}, 225);
    } else {
        Log.i("Mensaje", "Se tiene permiso para enviar/recibir SMS!");
    }
}

see more in this answer: link

    
answered by 07.11.2016 / 18:41
source