0

Possible Duplicate:
How do I pass data from a BroadcastReceiver through to an Activity being started?

In my Android application, I have a main activity called App and a BroadcastReceiver called SmsReceiver. Inside the onReceive() method of SmsReceiver, I want catch an SMS and send its contents back to App. I don't know how to do this, though. Would this be done with a class inside App? I have tried doing this, and adding that class's name to the AndroidManifest, but upon trying to call it, the application crashed with an error saying that this class could not be instantiated.

(I am aware that this question has been posted before, but I haven't seen any satisfying answers on them.)

Edit, to illustrate the solution I tried:

In the AndroidManifest, I added this:

    <receiver android:name=".App.SmsHandler" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

In the App class, I added:

public class SmsHandler extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        // Stuff here
    }
}

and

public static final String TEXT_INTENT = "textintent";

I tried to call it using the following code (in SmsReceiver but this made the app crash):

private void forwardSms(Message message, Context context) {
    Intent myIntent = new Intent(MsgWallApp.TEXT_INTENT);

    Bundle myData = new Bundle();
    myData.putString("sender", message.getSender());
    myData.putString("text", message.getText());
    context.sendBroadcast(myIntent);
}
Community
  • 1
  • 1
Neko
  • 3,550
  • 7
  • 28
  • 34

1 Answers1

2

If you only want to catch the event when your activity is not destroyed, than having the broadcast receiver inside the activity is enough.

If you want to catch all broadcasts, even when your activity is not started, have a look at Android Services.

About your specific error, how about pasting it here? Also maybe your code?

Radu
  • 2,076
  • 2
  • 20
  • 40