0

我正在尝试使用 android eclipse 创建 SMS 文本消息。我是android eclipse的初学者。我需要帮助来创建发送消息的方法。我无法在 2 个模拟器之间发送消息。有人可以帮我解决这个方法吗?谢谢!

主要的:

package com.example.sms;

import com.example.sms.R;

import android.os.Bundle;
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.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
Button sendSMS;
EditText msgTxt;
EditText numTxt;
IntentFilter intentFilter;


private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent)
{
    TextView inTxt = (TextView) findViewById(R.id.textMsg);
    inTxt.setText(intent.getExtras().getString("sms"));
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

intentFilter = new IntentFilter();
intentFilter.addAction("SMS_RECEIVED_ACTION");

sendSMS = (Button) findViewById(R.id.sendBtn);
msgTxt = (EditText) findViewById(R.id.message);
numTxt = (EditText) findViewById(R.id.numberTxt);
sendSMS.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        String myMsg = msgTxt.getText().toString();
        String theNumber = numTxt.getText().toString();

    }
});
}
protected void sendMSG(String theNumber, String myMsg) {
String SENT = "Message Sent";
String DELIVERED = "Message Delivered";

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

 registerReceiver(new BroadcastReceiver()
 {
 public void onReceive(Context arg0, Intent arg1)
 {
    switch(getResultCode())
    {
    case Activity.RESULT_OK:
 Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_LONG).show();
        break;
    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
 Toast.makeText(getBaseContext(), "Generic Failure",Toast.LENGTH_LONG).show();
        break;
    case SmsManager.RESULT_ERROR_NO_SERVICE:
 Toast.makeText(getBaseContext(), "No Service", Toast.LENGTH_LONG).show();
        break;      
    }
 }  
  }, new IntentFilter(SENT));

 registerReceiver(new BroadcastReceiver()
 {
 public void onReceive(Context arg0, Intent arg1)
 {
    switch(getResultCode())
    {
    case Activity.RESULT_OK:
 Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_LONG).show();
        break;
    case Activity.RESULT_CANCELED:
 Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_LONG).show();
        break;  
    }
 }
 }, new IntentFilter(DELIVERED));

 SmsManager sms = SmsManager.getDefault();
 sms.sendTextMessage(theNumber, null, myMsg, sentPI, deliveredPI);
 }

 protected void onResume(){
 registerReceiver(intentReceiver, intentFilter);
 super.onResume();
 }
protected void onPause(){
unregisterReceiver(intentReceiver);
super.onPause();    
}   
}
4

2 回答 2

0

使用此发送消息

SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage("1555215554", null, "message", null, null);

数字应为字符串格式。您可以在设置>关于手机>状态中看到模拟器的数量。

如果您感到困惑,可以查看此文档http://developer.android.com/reference/android/telephony/gsm/SmsManager.html#sendTextMessage(java.lang.String, java.lang.String, java.lang。字符串,android.app.PendingIntent,android.app.PendingIntent)

于 2013-10-29T05:59:30.237 回答
0

您可以使用以下内容发送:

 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("sms:"
                    + phoneNumber))); 

并在 2 个模拟器之间发送,您可以使用地址栏中显示的号码:通常它看起来像 5554,所以只需用它替换电话号码。

实际上有几种发送 SMS 的方法,其中一种是使用上述调用内置 SMS 应用程序的方法,或者您可以创建自己的方法,现在如果您想使用上述方法,只需将其放在您的 onClick() 实现中即可将调用内置短信应用程序,

但是,对于其他方法,请查看THIS nice and easy example

不要忘记在您的 Manifest.xml 中添加以下权限

<uses-permission android:name="android.permission.SEND_SMS" />
于 2013-10-29T05:47:35.620 回答