【问题标题】:Unable to send a message from one emulator to another无法从一个模拟器向另一个模拟器发送消息
【发布时间】:2015-11-28 20:25:35
【问题描述】:

即使我在目标地址中输入另一个模拟器的端口 ID,我也无法将文本从一个模拟器发送到另一个模拟器?

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

          }

【问题讨论】:

    标签: java android android-intent android-emulator smsmanager


    【解决方案1】:

    我最终从第二个模拟器启动了应用程序。也就是说,从5556它成功地向我启动的第一个模拟器发送了一条短信,到5554。我不知道为什么它不会从我启动的第一个模拟器发送。但是,它仍然有效。

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

    【讨论】:

      【解决方案2】:

      按如下方式进行:

      第 1 步:启动 AVD

      第 2 步:再次启动 AVD(例如,如果您第一次运行 AVD,它将分配像 5554 这样的端口,第二次可能是 5556)

      第 3 步:如果要从 5554 向 5556 发送消息,则需要转到 5554 模拟器,消息 -> 将手机号码写成 5556 并点击发送按钮。

      第 4 步:将以下权限添加到您的 AndroidManifest.xml

      第 5 步:写下以下代码并在模拟器 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 布局:

          <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>
      

      谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-29
        • 2017-06-29
        • 1970-01-01
        • 1970-01-01
        • 2022-12-06
        • 1970-01-01
        • 2014-08-26
        相关资源
        最近更新 更多