마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 문자보내기소스수정좀(밑에글과다른것임)

–2 추천
안드로이드 문자보내기를 하고잇는데요 지금이게 전화번호나 내용을 적어서 보내는 건데요 전화번호를 미리 저장된상태에서 내용만 적어서 보낼수잇도록 하고싶어요 소스수정좀부탁드려요ㅠㅠ

SmsSender .java-----------------------------------------------------------

 public class SmsSender extends Activity {
   
    Button btnSendSMS;
    EditText txtPhoneNo;
    EditText txtMessage;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sms_send);     
 
        btnSendSMS = (Button) findViewById(R.id.button);
        txtPhoneNo = (EditText) findViewById(R.id.phone);
        txtMessage = (EditText) findViewById(R.id.message);
 
      
        btnSendSMS.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
{
                String phoneNo = txtPhoneNo.getText().toString();
String message = txtMessage.getText().toString();
if (phoneNo.length()>0 && message.length()>0)
sendSMS(phoneNo, message);
                else
                    Toast.makeText(getBaseContext(),
                        "내용을 입력해 주십시오.",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    // SMS를 전송하는 과정을 모니터링하고 싶다면
    private void sendSMS(String phoneNumber, String message)
    {       
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";
 
        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
            new Intent(SENT), 0);
 
        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
            new Intent(DELIVERED), 0);

        //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(SENT));

        //---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
                }
            }
        }, new IntentFilter(DELIVERED));
       
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
    }
   
    // 모니터링 안하고 발송을 원한다면 아래 함수를 이용
    private void __sendSMS(String phoneNumber, String message)
    {       
        PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, SmsSender.class), 0);
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, pi, null);       
    }   

}

SmsReceiver .java--------------------------------------------------------

public class SmsReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();       
        SmsMessage[] msgs = null;
        String str = "";           
        if (bundle != null)
        {
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];           
            for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
                str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";       
            }
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        }
    }

}
익명사용자 님이 2014년 1월 22일 질문
저소스의xml입니다.
sms_send.xml---------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget28"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/label1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="받는사람 전화번호"
        android:textSize="12sp" >
    </TextView>

    <EditText
        android:id="@+id/phone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:phoneNumber="true"
        android:textSize="18sp" >
    </EditText>

    <TextView
        android:id="@+id/label2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="메시지"
        android:textSize="12sp" >
    </TextView>

    <EditText
        android:id="@+id/message"
        android:layout_width="fill_parent"
        android:layout_height="150px"
        android:gravity="top" >
    </EditText>

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="전송" >
    </Button>

</LinearLayout>

1개의 답변

0 추천
Uri sms = Uri.parse("smsto:");
Intent sendSms = new Intent(Intent.ACTION_SENDTO, sms);
sendSms.putExtra("sms_body", "TEST MESSAGE!");
startActivity(sendSms);

"TEST MESSAGE!" 부분에 입력받은 문자 넣어주시면 되겠네요

익명사용자 님이 2014년 1월 22일 답변
제질문은 그게아니라 실행을 햇을때 번호는 소스에 자동으로 저장되잇고
내용은 아무거나 칠수 있도록 만들고 싶다는겁니다ㅠㅠㅠ
...