Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling registerReceiver from a DialogFragment

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();        
    }

}

like image 844
daniel_c05 Avatar asked Sep 03 '12 05:09

daniel_c05


People also ask

What method is used to send a broadcast event?

Broadcasting Custom Intents If you want your application itself should generate and send custom intents then you will have to create and send those intents by using the sendBroadcast() method inside your activity class.

How broadcast receiver works in Android?

Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.

How to stop broadcast receiver in Android?

You can choose to "stop" a BroadcastReceiver either on, say a Button click, or perhaps in the onPause().


1 Answers

use

getActivity().registerReceiver(new BroadcastReceiver(){...}, new IntentFilter(SENT));

registerReceiver is a method of Context which is not present in DialogFragment

like image 75
nandeesh Avatar answered Oct 16 '22 09:10

nandeesh