I want a toast message when sms is received.
I tried to explicitly specify the intent filter in the manifest, and I partially succeeded, except that I do not want to call it when the application is killed, so, on the advice of some programmers here, I tried to make the broadcast receiver programmatically by placing two buttons in my user interface, namely, registerand unregistertherefore their sole purpose is to register and unregister the broadcast receiver.
My main goal is to start the broadcast receiver, even if the application is background (it meets the condition, the user clicked on the registration button after using any other application).
I used this tutorial: http://www.javacodegeeks.com/2012/09/android-broadcast-receiver.html , but basically changed it using the sms receiver.
here is my code:
package gates.apps.automaticmessageresponder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
SmsReceiver broadcastReceiver=new SmsReceiver();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void register(View view){
this.registerReceiver(broadcastReceiver, new IntentFilter(
"android.provider.Telephony.SMS_RECEIVED"));
Log.e("register","pressed");
}
public void unRegister(View view){
this.unregisterReceiver(broadcastReceiver);
Log.e("unregister","pressed");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And another class
package gates.apps.automaticmessageresponder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
@Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
toast.show();
}
}
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
}
I did not change the manifest file except that I added permission to receive SMS . Even I enter the log, the register is pressed and not pressed. I think the problem is that the register receiver button is called even without a click, is this abnormal behavior? or is my perception wrong?