Question

I'm attempting to create an Android application that accomplishes the following: it sends an initial survey question via text message to a group of recipients (numbers stored in an array). Then for each response it gets, it sends that participant the next question in the series. This is the first Android development I've ever done, so my apologies if this is obvious!

I have the sendQuestion function defined as such within my MainActivity:

public void sendQuestion(String number, int question)
{
    //necessary setup for sending a text message
    SmsManager sms = SmsManager.getDefault();
    PendingIntent sentPI;
    String SENT = "SMS_SENT";
    sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);

    //actually sends the text message
    sms.sendTextMessage(number, null, questions[question], sentPI, null);
}

Then I'm attempting to call the sendQuestion function from within my SmsReceiver class:

public class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        for (int i=0;i<MainActivity.numbers.length;i++)
        {
            MainActivity.sendQuestion(MainActivity.numbers[i], MainActivity.survey_location[i]);
        }
    }
}

The first error I get is that I can't make a static reference to the non-static method sendQuestion from type MainActivity. As a fix, I thought I'd try changing sendQuestion to be a static method. But then I get the error on sendPI within sendQuestion that I cannot use this in a static context.

Is there a way I can get MainActivity's context OR keep sendQuestion non-static without conflicting with whichever one I don't change? And what's the best way to avoid this type of problem in general in the future?

Was it helpful?

Solution

Recommended way would be to make utility methods like sendQuestion static in a different class and send the context as a parameter to the method. In that way you can access this method from any where and you just need to pass the context as additional parameter to the method.

OTHER TIPS

I don't see where you construct the SmsReceiver. If you do this from somewhere in MainActivity, you could add a MainActivity parameter to the constructor:

public class SmsReceiver extends BroadcastReceiver {

    private MainActivity mActivity;

    public SmsReceiver(MainActivity activity) {
        super();
        mActivity = activity;
    }

and in MainActivity, when you create the new SmsReceiver, it could be something like

recv = new SmsReceiver(this);

and then, instead of MainActivity.sendQuestion, you'd call mActivity.sendQuestion. I don't know enough about your program, so the details may differ. The main thing, though, is that your SmsReceiver needs a reference to a MainActivity object so that it knows what object to call sendQuestion for; therefore, the rest of the program needs a way to give it the reference it needs. And giving it the information as a constructor parameter is probably the simplest way to accomplish that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top