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