EDIT
I added this line:
getActivity().registerReceiver(new BroadcastReceiver(){...}, new IntentFilter(SENT));
There is now a runtime error though, the error reads:
09-03 04:12:41.856: E/AndroidRuntime(1692): java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.ImageView with id 'button_send'
My question would be: Why would it be looking for the method in android.view.ContextThemeWrapper?
The button is created on the layout we referenced on line:
View view = inflater.inflate(R.layout.activity_composer, container);
and the "button_send" has an onClick method set as:
android:onClick="sendMessage"
Which is later defined as:
public void sendMessage (View v) {
String phoneNumber = number_input.getText().toString();
String message = body_input.getText().toString();
if(message.length() > 0) {
sendSMS(phoneNumber, message);
}
sendSMS(phoneNumber, message);
clearForm();
}
The runtime error happens when the button to send the message is clicked.
END OF EDIT
EDIT Number Two
Apparently you cannot define the onClick method inside your XML since the button is part of the fragment. In this case, I had to set an onClickListener
Issue FIXED, thanks!
So I am trying to create a DialogFragment that simply sends a new text message. The Dialog has two EditText views, one for the phone number, and one for the actual message; it also has 1 ImageButton that has an onClick method called sendMessage (View v). With that said, I am having issues registering a receiver to catch the result code when the message is being sent. The error message is:
"The method registerReceiver(new BroadcastReceiver(){}, IntentFilter) is undefined for the type ComposerDialog"
With exactly the same blocks of code, excepting onCreateView(), I can make this work on an Activity, but apparently not on a DialogFragment. My guess is that on the line:
PendingIntent sentPI = PendingIntent.getBroadcast(getActivity(), 0,
new Intent(SENT), 0);
by calling getActivity(), I am passing a Dialog as the context, and apparently that is not a parameter the call to registerReceiver recognizes on line:
registerReceiver(new BroadcastReceiver(){...}, new IntentFilter(SENT));
I am thinking of how to change the context I pass to the registerReceiver call, but I am not even sure that is actually the problem. Any ideas?
Here is the full Class for a better perspective:
package com.deadpixels.test.sms;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class ComposerDialog extends DialogFragment {
public ComposerDialog () {
}
private EditText body_input;
private EditText number_input;
SmsManager smsMgr = SmsManager.getDefault();
private static final String TAG = "Composer";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_composer, container);
body_input = (EditText) view.findViewById(R.id.text_input);
number_input = (EditText) view.findViewById(R.id.text_address);
getDialog().setTitle("Create your new Message!");
return view;
}
private void clearForm() {
body_input.setText("");
number_input.setText("");
}
public void sendMessage (View v) {
String phoneNumber = number_input.getText().toString();
String message = body_input.getText().toString();
if(message.length() > 0) {
sendSMS(phoneNumber, message);
}
sendSMS(phoneNumber, message);
clearForm();
}
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
PendingIntent sentPI = PendingIntent.getBroadcast(getActivity(), 0,
new Intent(SENT), 0);
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Log.v(TAG, "SMS sent succesfully");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Log.v(TAG, "Generic failure");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Log.v(TAG, "No service");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Log.v(TAG, "Null PDU");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Log.v(TAG, "Radio off");
break;
}
}
}, new IntentFilter(SENT));
smsMgr.sendTextMessage(phoneNumber, null, message, sentPI, null);
clearForm();
}
}
use
getActivity().registerReceiver(new BroadcastReceiver(){...}, new IntentFilter(SENT));
registerReceiver
is a method of Context which is not present in DialogFragment