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>