设置发送消息的方法

时间:2013-10-29 05:40:17

标签: android eclipse methods

我正在尝试使用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();    
}   
}

2 个答案:

答案 0 :(得分:0)

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

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

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

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

但是对于其他方法,请查看THIS简单易用的示例

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

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

答案 1 :(得分:0)

使用此

发送消息
SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage("1555215554", null, "message", null, null);

该数字应为String格式。您可以在设置&gt;关于手机&gt;状态时看到模拟器的数量。

如果您混淆http://developer.android.com/reference/android/telephony/gsm/SmsManager.html#sendTextMessage(java.lang.String, java.lang.String, java.lang.String, android.app.PendingIntent, android.app.PendingIntent)

,可以查看此文档