无法从一个模拟器向另一个模拟器发送消息

时间:2015-09-03 06:17:50

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

即使我在目标地址输入另一个模拟器的端口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();

          }

2 个答案:

答案 0 :(得分:0)

我最终从第二个模拟器启动了应用程序。也就是说,从5556开始,它成功地将短信发送到我开始的第一个模拟器,到5554.我不知道为什么它不会从我开始的第一个模拟器发送。但尽管如此,它还是有效的。

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

答案 1 :(得分:0)

按如下方式进行:

第1步:启动AVD

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

步骤3:如果要从5554发送消息到5556,则需要转到模拟器5554,消息 - &gt;撰写并将手机号码写为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>

由于