Call Activity Method From Broadcast Receiver Android?
Solution 1:
I was able to solve it by declaring receiver programmatically:
In the activity befor sending the message
privatevoidsendSMS() {
BinarySMSReceiversmsReceiver=null;
smsReceiver = newBinarySMSReceiver();
smsReceiver.setActivityHandler(this);
IntentFilterportIntentFilter=newIntentFilter("android.intent.action.DATA_SMS_RECEIVED");
portIntentFilter.addDataAuthority("*", "9512");
portIntentFilter.addDataScheme("sms");
registerReceiver(smsReceiver, portIntentFilter);
StringmessageText= etMobile.getText().toString().trim();
shortSMS_PORT=9512;
SmsManagersmsManager= SmsManager.getDefault();
smsManager.sendDataMessage(etMobile.getText().toString().trim(), null, SMS_PORT, messageText.getBytes(), null, null);
}
In receiver class
publicclassBinarySMSReceiverextendsBroadcastReceiver {
VerifyActivityvAct=null;
voidsetActivityHandler(VerifyActivity main) {
vAct = main;
}
@OverridepublicvoidonReceive(Context context, Intent intent) {
Bundlebundle= intent.getExtras();
SmsMessage[] msgs = null;
if (null != bundle) {
Stringinfo="SMS from ";
Stringsender="";
Stringmsg="";
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = newSmsMessage[pdus.length];
byte[] data = null;
for (inti=0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
sender += msgs[i].getOriginatingAddress();
info += msgs[i].getOriginatingAddress() + "\n";
data = msgs[i].getUserData();
for (intindex=0; index < data.length; ++index) {
info += Character.toString((char) data[index]);
msg += Character.toString((char) data[index]);
}
}
Log.e("message", "receiver " + msg);
Log.e("sender", "from " + info);
vAct.msgReceived(msg); //activity method
}
}
}
Unregister the receiver
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(smsReceiver);
}
Solution 2:
The error you get is that you are wrongly casting a android.app.ReceiverRestrictedContext
to a com.vfi.VerifyActivity
.
If you want to achieve what you want to do simply start your activity by giving it some extra information.
publicclassBinarySMSReceiverextendsBroadcastReceiver {
@OverridepublicvoidonReceive(Context context, Intent intent) {
Bundlebundle= intent.getExtras();
SmsMessage[] msgs = null;
if (null != bundle) {
Stringinfo="SMS from ";
Stringsender="";
Stringmsg="";
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = newSmsMessage[pdus.length];
byte[] data = null;
for (inti=0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
sender += msgs[i].getOriginatingAddress();
info += msgs[i].getOriginatingAddress() + "\n";
data = msgs[i].getUserData();
for (intindex=0; index < data.length; ++index) {
info += Character.toString((char) data[index]);
msg += Character.toString((char) data[index]);
}
}
Log.e("SakjsdMS", "akjsdhkas" + msg);
Log.e("sender", "asdasdasdasdasdasd" + info);
// HERE COMES THE CHANGEIntentintent=newIntent(context, YourActivityToLaunch.class);
intent.putExtra("message_received", msg);
context.startActivity(intent);
}
}
}
And in your other class simply retrieve your message this way :
Intentintent= getIntent();
Stringmessage= intent.getStringExtra("message_received");
That's it.
Hope this help !
Solution 3:
The Context
that your BinarySMSReceiver
is passed in onReceive
is not an Activity - it is the Context in which the receiver is running. To start an Activity, you need to use context.startActivity(Intent)
and pass any additional data to the Activity using the Intent.
Solution 4:
that ((VerifyActivity)context).msgReceived(msg);
that's your mistake.
Why are you assuming that this context is your activity?
The best way (without 3rd party libraries) to do this is to send a local broadcast.
On this other answer I gave a general idea on how to use LocalBroadcast
Best practice to launch AsyncTask from custom view
if you're Ok using 3rd party libraries I suggest you check Otto
from Square
Solution 5:
Your BroadcastReceiver doesn't know anything about any activity by default. You will have to give the context to it, for example in your constructor when you create the broadcastReceiver.
private Activity activity;
publicBroadcastReceiver(Activity activity) {
super();
this.activity = activity;
}
In you onReceive method you will now be able to use your activity in whatever way you like.
Post a Comment for "Call Activity Method From Broadcast Receiver Android?"