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

I am trying to create a text messaging application in android, the code works, but the only thing that is wrong is that I can not send messages to the emulator using DDMS. I pasted my code below, in case it is necessary, and I have a screenshot right 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 into 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's what the DDMS output looks like: enter image description here The main problem here is that the switches for telephony actions are not clickable. Someone please help me fix this.

+5
source share
4 answers

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

 Ctrl + Shift + P 

For other options click here.

+16
source

You can also send via the terminal. answered it @ Andy Crush Sending and receiving text using the Android emulator

make sure the emulator is online adb devices

  • Open the console (terminal)
  • Connect via telnet to the running emulator: telnet localhost 5554 (you can find the port number in the header of the emulator)
  • enter this: sms send senderPhoneNumber textmessage

UPDATED: Another tutorial: mobiforge: SMS messages in Android

+6
source

You can send using the advanced elements of the emulator, working fine for me, I could not send using DDMS You can send using advanced emultor controls

+4
source

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

If you donโ€™t help, close Eclipse and run DDMS outside of Eclipse

Go to the ddms script shell in the tool directory where you installed the Android SDK.

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

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

0
source

All Articles