简体   繁体   中英

Unable to send a message from one emulator to another

I'm not able to send a text from one emulator to another even when I enter the port id of another emulator in the destination address ?

public void a(View v)
   {

       Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI); 
       int PICK_CONTACT=0;
       startActivityForResult(intent, PICK_CONTACT);

   }


   private void sendSMSMessage() 

   {

       Log.i("Sms Sent", "");
          String phoneNo = edt1.getText().toString();
          String message = edt2.getText().toString();

          Intent smsIntent = new Intent(Intent.ACTION_VIEW);
          smsIntent.setData(Uri.parse("smsto:"));
          smsIntent.setType("vnd.android-dir/mms-sms");
          smsIntent.putExtra("address"  , new String("0123456789"));
          smsIntent.putExtra("sms_body"  , "Test");



          try
          {

             SmsManager sm = SmsManager.getDefault();
             sm.sendTextMessage(phoneNo, null, message, null, null);
             Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();

          } 

          catch (Exception e) 
          {

             Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show();
             e.printStackTrace();

          }

I ended up starting the application from the second emulator. That is, from 5556 and it successfully sent an sms to the first emulator I started, to 5554. I don't know why it would not send from the first emulator I started. But nonetheless, it worked.

<uses-permission android:name="android.permission.SEND_SMS" />

Do it as follows:

Step 1 : Start AVD

Step 2 : Again Start AVD (for example if you run AVD First time it will be assign port like 5554 and second time port may be 5556)

Step 3 : If you want to send message from 5554 to 5556 then you need to goto emulator 5554, Message -> Compose and write mobile number as 5556 and message hit Send Button.

Step 4 : Add following permission into your AndroidManifest.xml

Step 5 : Write down following code and Run your project in emulator 5554

youractivity extends Activity {

    PendingIntent pi;
    SmsManager sms;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        //======================================
        //  Get all pending Activity
        //======================================
        String msg = "android.telephony.SmsManager.STATUS_ON_ICC_SENT";
        PendingIntent piSent = PendingIntent.getBroadcast(MainActivity.this, 0,new Intent(msg), 0);

        //======================================
        //  Send SMS Using Default SMS Manager
        //======================================
                sms = SmsManager.getDefault();
                sms.sendTextMessage("5556", null, "This is sample test message", piSent, null);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

activity_main xml layout:

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="45dp"
        android:gravity="center_horizontal"
        android:text="Send Message to emulators programmatically"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM