How to register broadcast receiver?

You need to move your receiver outside the onCreate. Something like public class MainActivity extends Activity { private static String content; private static String phone; private String number; private String message; private BroadcastReceiver receiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { //---get the SMS message passed in--- Bundle bundle = intent.getExtras(); SmsMessage msgs = null; if (bundle! = null) { number = ""; message = ""; //---retrieve the SMS message received--- Object pdus = (Object) bundle.

Get("pdus"); msgs = new SmsMessagepdus. Length; for (int i=0; iCreateFromPdu((byte)pdusi); number = msgsi. GetOriginatingAddress(); message = msgsi.getMessageBody(); } //---display the new SMS message--- Toast.

MakeText(context, message, Toast. LENGTH_SHORT).show(); SendMe(); } } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.

OnCreate(savedInstanceState); IntentFilter filter = new IntentFilter(); filter. AddAction(YOUR_SMS_ACTION); this. RegisterReceiver(this.

Receiver, filter); setContentView(R.layout. Main); } public void SendMe(){ PendingIntent pi = PendingIntent. GetActivity(this, 0, new Intent(this, MainActivity.

Class), 0); SmsManager sms = SmsManager.getDefault(); sms. SendTextMessage(number, null, message, pi, null); } }.

You need to move your receiver outside the onCreate. Something like - public class MainActivity extends Activity { private static String content; private static String phone; private String number; private String message; private BroadcastReceiver receiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { //---get the SMS message passed in--- Bundle bundle = intent.getExtras(); SmsMessage msgs = null; if (bundle! = null) { number = ""; message = ""; //---retrieve the SMS message received--- Object pdus = (Object) bundle.

Get("pdus"); msgs = new SmsMessagepdus. Length; for (int i=0; iCreateFromPdu((byte)pdusi); number = msgsi. GetOriginatingAddress(); message = msgsi.getMessageBody(); } //---display the new SMS message--- Toast.

MakeText(context, message, Toast. LENGTH_SHORT).show(); SendMe(); } } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.

OnCreate(savedInstanceState); IntentFilter filter = new IntentFilter(); filter. AddAction(YOUR_SMS_ACTION); this. RegisterReceiver(this.

Receiver, filter); setContentView(R.layout. Main); } public void SendMe(){ PendingIntent pi = PendingIntent. GetActivity(this, 0, new Intent(this, MainActivity.

Class), 0); SmsManager sms = SmsManager.getDefault(); sms. SendTextMessage(number, null, message, pi, null); } }.

Suchi okay I think that will do it. Do I need to call anything in my onCreate for it? – theITRanger22 Jun 28 at 15:19 I have added the registerReceiver call in the onCreate.

That is working for me. Let me know if it works out! – Suchi Jun 28 at 15:21 @Suchi =( Im still getting a force close after that.

Maybe its something im doing wrong – theITRanger22 Jun 28 at 15:30 1 check the edit. Added an intentfilter. See if that works – Suchi Jun 28 at 15:33 @Suchi I just pasted my code that force closes in the bottom of your answer.

Thanks for all your help. – theITRanger22 Jun 28 at 15:33.

The force close is likely happening because you are managing the UI from within your broadcast receiver. There's a 10-second limit on a BR's onReceive before it is forced closed. To solve, use an Activity component to generate your Toast.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions