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