Alex Irabor
Alex Irabor

Reputation: 401

How send a message to the emulator using DDMS in Android studio

i am trying to build a text messaging application in android, the code works, but the only thing wrong about it is that i cannot send messages to the emulator using DDMS. I have pasted my code below, just in case it is necessary, and i have the screen shot immediately after

package com.example.oghenekaroedoh.sms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by OGHENEKARO EDOH on 15/04/2015.
 */
public class SMSReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent)
    {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "SMS from ";
        if (bundle != null)
        {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                if (i==0) {
                    //---get the sender address/phone number---
                    str += msgs[i].getOriginatingAddress();
                    str += ": ";
                }
                //---get the message body---
                str += msgs[i].getMessageBody().toString();
            }
            //---display the new SMS message---
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
            Log.d("SMSReceiver", str);
        }
    }
}

Here is the code for the Activity, paste this code in another file..

package com.example.oghenekaroedoh.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.telephony.SmsManager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by ALEX IRABOR on 16/04/2015.
 * NOTES..
 * This code illustrates how to create send and receive sms, but unlike the others, the messages are displayed in a view
 * for more information on this code check out page 334-338 beginning android 4 application development
 */
public class SMSActivity3 extends Activity {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";
    PendingIntent sentPI, deliveredPI;
    BroadcastReceiver smsSentReceiver, smsDeliveredReceiver;
    IntentFilter intentFilter;

        private BroadcastReceiver intentReceiver = new BroadcastReceiver() {

            public void onReceive(Context context, Intent intent) {
                //---display the SMS received in the TextView---
                TextView SMSes = (TextView) findViewById(R.id.textView1);
                SMSes.setText(intent.getExtras().getString("sms"));
            }
        };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);

        sentPI = PendingIntent.getBroadcast(this, 0,
                new Intent(SENT), 0);

        deliveredPI = PendingIntent.getBroadcast(this, 0,
                new Intent(DELIVERED), 0);

        //---intent to filter for SMS messages received---
        intentFilter = new IntentFilter();
        intentFilter.addAction("SMS_RECEIVED_ACTION");
    }

    @Override
    public void onResume(){
        super.onResume();

        //---register the receiver---
        registerReceiver(intentReceiver, intentFilter);

        /*A broadcastReceiver receives broadcast from any of the pendingIntents created. So when
         sentPI sends a broadcast, it knows that the message has been sent, and when the
         deliveredPI sends a broadcast, it knows that the message has been delivered..
        * */

        //---create the BroadcastReceiver when the SMS is sent---
        smsSentReceiver = new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS sent",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off",
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };
        smsDeliveredReceiver = new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };
        //---register the two BroadcastReceivers---
        registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED));
        registerReceiver(smsSentReceiver, new IntentFilter(SENT));
    }

    public void onPause(){
        super.onPause();
        //---unregister the receiver---
        unregisterReceiver(intentReceiver);

        //---unregister the two BroadcastReceivers---
        unregisterReceiver(smsSentReceiver);
        unregisterReceiver(smsDeliveredReceiver);
    }

    public void onClick(View v) {
        sendSMS("5556", "Hello my friends!");
    }

    public void onClick2 (View v) {
        Intent i = new
                Intent(android.content.Intent.ACTION_VIEW);
        i.putExtra("address", "5556; 5558; 5560");
        i.putExtra("sms_body", "Hello my friends!");
        i.setType("vnd.android-dir/mms-sms");
        startActivity(i);
    }

    //—-sends an SMS message to another device—-
    private void sendSMS(String phoneNumber, String message)
    {
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
    }


}

here is how the DDMS output looks like: enter image description here The main problem here is that the radio buttons for the telephony actions are not clickeable. Someone please help me fix this

Upvotes: 9

Views: 22033

Answers (4)

Shahzain ali
Shahzain ali

Reputation: 1725

You can send using emulator extended controls, working perfectly for me, I was unable to send using DDMS You can send using emultor Extended Controls

Upvotes: 9

Prashant Sharma
Prashant Sharma

Reputation: 1407

There is no need use DDMS to send sms to emulator. You have to hit the following keys:-

Ctrl + Shift + P

For other options click here

Upvotes: 30

Bharatesh
Bharatesh

Reputation: 9009

You can also send through terminal. its answered by @Andi Krusch Sending and receiving text using android emulator

make sure you emulator is online adb devices

  1. Open a console (Terminal)
  2. Connect via telnet to the running emulator: telnet localhost 5554 (you can find the portnumber in the title of the emulator)
  3. type this: sms send senderPhoneNumber textmessage

UPDATED : Another tutorial : mobiforge : SMS Messaging in Android

Upvotes: 8

Jain Nidhi
Jain Nidhi

Reputation: 255

you have the device is selected in the Devices view. without device or emulator You cannot use Emulator Control.

If not help, close Eclipse, and run DDMS outside of Eclipse

Go to the ddms shell script in tools directory wherever you installed the Android SDK.

If it not work then help, run adb kill-server, followed by adb start-server, where adb should be in your platform-tools/ directory of your SDK installation. Then, try DDMS again.

If that does not help, reboot, then try DDMS again.

Upvotes: 0

Related Questions